question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add "\" or "{" into a tex file?

See original GitHub issue

I am a newbie of Python and LaTex. Now I have designed a .tex file. Then I want use PyLaTex (version 1.0.0 )to generate this .tex file and compile it with xelatex .There are some questions confused me for a few days.I couldn’t get answers by myself ,nor the search engine. Please help me, thanks a lot.

  • How to set the documentclass with some variables? Such as
\documentclass[11pt,a4paper]{article}
  • How to add some text with \ or {} into the tex file? I tried doc.packages.append and doc.append, but both of them will change \ to \textbackslash{} and change { to \{ which make the tex compile fail. I added r'' or u'' didn’t work. I want to append some text into tex such as
\definecolor{ColorName}{RGB}{76,73,72}  
\setCJKmainfont{SimHei} 
\setCJKmonofont{KaiTi}  
\setmainfont{Times New Roman}

If I typed doc.packages.append(r'\definecolor{ColorName}{RGB}{76,73,72}') it will generate \textbackslash{}definecolor\{ColorName\}\{RGB\}\{76,73,72\}

  • How to append \vspace and \hspace into a figure? To make the tex like
\begin{figure}[h!]
\vspace{17.634mm}\hspace*{54.314mm}
\includegraphics[width=10.3977cm]{title.jpg}
\end{figure}

Thanks a lot. PyLaTex helped me a lot , but I couldn’t get enough documents. Waiting for your reply.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
krietecommented, Aug 4, 2016

Hey xiuyanduan, I am using pylatex since while and just saw this issue, maybe I can help you.

How to set the documentclass with some variables?

  • You can define the document class by using the pylatex Options from pylatex.base_classes.command import Options and set the document class like this: doc.documentclass.options=Options('12pt', 'svgnames', 'final')

How to add some text with \ or {} into the tex file?

  • I think your problem is that firstly python escapes the backslashes and pylatex also in some occurences. You can avoid this by following the basic example provided in the pylatex examples using the pylatex NoEscape function with the prefix r before your string.

See here e.g.: basic example here it’s used like this:

doc.append(NoEscape(r’\maketitle’))

In your case I would suggest you use doc.preamble.append(NoEscape(r'\definecolor{ColorName}{RGB}{76,73,72}'))

Note that you have to import the NoEscape definition first: from pylatex import NoEscape

How to append \vspace and \hspace into a figure?

  • Using the python with command you are basically inside the figure environment and using doc.append(NoEscape(r'')) should do the trick (I guess)
with doc.create(Figure(position='htbp')) as plot:
    doc.append(NoEscape(r''))
    plt.plot(x,y)
    plot.add_plot(width=NoEscape(r'1\textwidth'))

Hope that helps to solve your problem. Let us know how this works out for you.

By the way, if you don’t do this already, you can let pylatex generate your pdf document by e.g. calling doc.generate_pdf(extra_compiler_args=‘–xelatex’)

Regards, Andreas

1reaction
JelteFcommented, Aug 4, 2016

@kriete thanks for replying, your solutions should work. Some slight aditions. Another option is to use a Command object, for the definecolor command instead of a raw string. This has as an advantage that is less error prone to typos as the latex code is generated. Also arguments are escaped by default so user input can safely be used. https://jeltef.github.io/PyLaTeX/latest/pylatex/pylatex.base_classes.command.html#pylatex.base_classes.command.Command

To set the options on the documentclass you can also pass in a Command object as the documentclass parameter for the Document. You can then set the options there.

The vspace and hspace question should probably not be done by adding an empty string. I think you meant this:

doc.append(NoEscape(r'\vspace{17.634mm}\hspace*{54.314mm}'))

Again, this can be done with a Command object.

Important to note for the xelatex solution is that this requires that latexmk is installed, which is highly advised as it compiles latex, biblatex and others as much as necessary. If you want to not use latexmk anyway you can simply use the compiler argument and set it to xelatex. (also, the argument is called compiler_args instead of extra_compiler_args)

Read more comments on GitHub >

github_iconTop Results From Across the Web

When should I use \input vs. \include? - LaTeX Stack Exchange
Command \input inserts the file contents without either \clearpage. It generates a LaTeX error on compile, if the file does not exist. By...
Read more >
Management in a large project - Overleaf, Online LaTeX Editor
The standard tools to insert a LaTeX file into another are \input and \include . ... Use this command in the document body...
Read more >
How do I create my first document in LaTeX?
Write text with LaTeX markup code in text editor -> produces 'paper.tex' · Run 'latex' on this source -> produces 'paper.dvi' · Preview...
Read more >
Compiling a \(\LaTeX\) Document - How to Use LaTeX
Using the Command Line/Terminal · latex [filename].tex will compile [filename].tex and output the file [filename].dvi · pdflatex [filename].tex ...
Read more >
How to print Stata outputs and .tex files - Stack Overflow
Otherwise, just put what you have in the document environment, and add \usepackage{booktabs} in the preamble (if not present).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found