How to add "\" or "{" into a tex file?
See original GitHub issueI 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 thetex
file? I trieddoc.packages.append
anddoc.append
, but both of them will change\
to\textbackslash{}
and change{
to\{
which make thetex
compile fail. I addedr''
oru''
didn’t work. I want to append some text intotex
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 thetex
like
\begin{figure}[h!]
\vspace{17.634mm}\hspace*{54.314mm}
\includegraphics[width=10.3977cm]{title.jpg}
\end{figure}
- Where could I get more documents except https://jeltef.github.io/PyLaTeX/latest/index.html
Thanks a lot. PyLaTex
helped me a lot , but I couldn’t get enough documents.
Waiting for your reply.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey xiuyanduan, I am using pylatex since while and just saw this issue, maybe I can help you.
from pylatex.base_classes.command import Options
and set the document class like this:doc.documentclass.options=Options('12pt', 'svgnames', 'final')
See here e.g.: basic example here it’s used like this:
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
with
command you are basically inside the figure environment and usingdoc.append(NoEscape(r''))
should do the trick (I guess)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
@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.CommandTo set the options on the documentclass you can also pass in a
Command
object as thedocumentclass
parameter for theDocument
. 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:
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 uselatexmk
anyway you can simply use thecompiler
argument and set it toxelatex
. (also, the argument is calledcompiler_args
instead ofextra_compiler_args
)