Change "Continued on next page" in df.to_latex / PYLATEX

134 views Asked by At

I would like to change the phrase "Continued on next page" from df.to_latex.

When running

import pylatex to pl
pl.NoEscape(dff1.to_latex(longtable=True, index=False, multicolumn=True, multirow=True,caption='Tabla de anexos'))

it turns out this:

\begin{longtable}{lrrrrrr}
\caption{Puntos criticos expuestos a peligro}\\
\toprule
{} & \multicolumn{3}{l}{ANA} & \multicolumn{3}{l}{INGEMMET} \\
{} & Nivel 2 & Nivel 3 & Nivel 4 &  Nivel 2 & Nivel 3 & Nivel 4 \\
\midrule
\endfirsthead
\caption[]{Puntos criticos expuestos a peligro} \\
\toprule
{} & \multicolumn{3}{l}{ANA} & \multicolumn{3}{l}{INGEMMET} \\
{} & Nivel 2 & Nivel 3 & Nivel 4 &  Nivel 2 & Nivel 3 & Nivel 4 \\
\midrule
\endhead
\midrule
\multicolumn{7}{r}{{Continued on next page}} \\
\midrule
\endfoot

\bottomrule
\endlastfoot
Ancash       & 13 & 0 & 0 & 113 & 0 & 0 \\
Apurimac     & 9 & 0 & 0 & 39 & 0 & 0 \\
Arequipa     & 2 & 0 & 0 & 13 & 0 & 0 \\
Ayacucho     & 34 & 0 & 0 & 28 & 0 & 0 \\
Cusco        & 59 & 0 & 0 & 68 & 0 & 0 \\
Huancavelica & 43 & 0 & 0 & 60 & 0 & 0 \\
Hutnuco      & 13 & 0 & 0 & 0 & 0 & 0 \\
Junin        & 16 & 0 & 0 & 0 & 0 & 0 \\
Pasco        & 17 & 0 & 0 & 23 & 0 & 0 \\
Puno         & 7 & 0 & 0 & 2 & 0 & 0 \\
Huanuco      & 0 & 0 & 0 & 32 & 0 & 0 \\
Junmn        & 0 & 0 & 0 & 41 & 0 & 0 \\
Lima         & 0 & 0 & 0 & 61 & 0 & 0 \\
\end{longtable}

And I'm looking for \multicolumn{7}{r}{{Continued on next page}} \ be changed by \multicolumn{7}{r}{{Siguiente pagina}} \

I tried pl.NoEscape(df_zc_ana_ingmmet.to_latex(longtable=True,index=True,multicolumn=True,multirow=True, caption='Puntos criticos expuestos a peligro',multicolumn_format='{7}{r}{{Continua en la siguiente pagina}}')), but I didn't succeed.

1

There are 1 answers

2
Jan On

I think I understand the problem, as I had the same: the mentioned pandas function ".to_latex" automatically inserts the phrase "Continued on next page" in English in the LaTeX output. This is a problem, when you want to insert the LaTeX longtable in a document written in a different language than English (like Spanish in the example above or German in my case).

So if I get the question right, it’s: how can I change this phrase to another (with the same meaning, but in a different language)?

Actually, I did not find an elegant solution. So my quick-and-dirty solution was to open the file after writing the English phrase and change the phrase to German:

file_path = "path/filename.tex"
search_phrase = "Continued on next page"
replacement_phrase = "Fortsetzung auf der nächsten Seite" # or use "Siguiente pagina" here

# Read the contents of the file
with open(file_path, 'r', encoding='utf-8') as file:
    file_content = file.read()

# Replace the search phrase with the replacement phrase
updated_content = file_content.replace(search_phrase, replacement_phrase)

# Write the updated content back to the file
with open(file_path, 'w', encoding='utf-8') as file:
    file.write(updated_content)

Although it is not very elegant, at least it worked for me. Perhaps somebody else has some more elegant solution?

For transparency: for coding rapidly, I used ChatGPT 3.5