74
votes
How to highlight Python syntax in LaTeX Listings \lstinputlistings command
I found this python package pythonhighlight on Github
Define it like this \usepackage{pythonhighlight} and use it like this:
\begin{python}
def f(x):
return x
\end{python}
18
votes
Accepted
Automatic document generation based on a database
Following Jim's suggestion, I wrote down an Python-code that does what I wanted to do.
Even if it is more Python oriented, I think it might be interesting to post the answer here.
(Since we're TeXer ...
15
votes
Accepted
How to use the \siunitx package within Python/matplotlib?
This is probably not the correct place to ask this, as the question is about Matplotlib and Python not about LaTeX, IMHO. I use siunitx in the following way:
pgf_with_latex = { # ...
15
votes
Accepted
Export Dataframe to Table in Latex
The to_latex() makes a finished tabular, so you just need to write that to a file, and use \input in your LaTeX document.
Assuming Python 3, here is a minimal Python example to make a DataFrame and ...
14
votes
Color coding in Python f-strings
I couldn't find an answer using the listings package.
However, I reckon the minted package understands Python f-strings:
\documentclass{article}
\usepackage{minted}
\begin{document}
\begin{minted}{...
13
votes
How to present a Python code snippet efficiently in LaTeX?
Here is the way I like to do. Of course you can change colors, font sizes, borders and so on.
You need Pygments to use minted.
MWE
\documentclass[11pt,a4paper]{report}
\usepackage{tcolorbox}
\...
12
votes
Accepted
How to check if a string is a valid LaTex rule?
On Linux at least (don't know about Windows), there is the latexdef script by Martin Scharrer, which looks up LaTeX definitions from the command line:
latexdef section
will print
\section
\long ...
10
votes
How to influence the size of a tikzpicture generated from Python using tikzplotlib?
In version 0.9.3 you seem to need axis_height and axis_width, rather than figureheight and figurewidth.
9
votes
Accepted
produce copy-paste-able pdf output with correct indentation with listing
Inho it is not really possible in a reliable way. It depends too much on the pdf viewer. It is possible to insert real spaces with the \pdffakespace primitive, but the indentation is preserved in ...
9
votes
Accepted
PythonTEX not working on current Overleaf compiler
(Tom from Overleaf support here.)
Unforunately, the pythontex package is not currently supported on Overleaf v2. This is because of a change to the security model in Overleaf v2 (as opposed to v1).
...
9
votes
Can I execute python code inside LaTeX
You could use pythontex or just call python by hand, here is python running on overleaf calculating 1+2=3 (there are simpler ways of adding)
The tex code (which needs --shell-escape but that is ...
8
votes
Using Python within LaTeX and accessing its variables
A comparison between all the options.
package name
LaTeX
CTAN
pers.
sync
1-run
shell
depython?
engine.
exp.
overleaf
version
note
⟨column header long name⟩
is it really LaTeX?
is it on CTAN?
...
8
votes
Accepted
How to influence the size of a tikzpicture generated from Python using tikzplotlib?
As described in the Readme, tikzplotlib has the possibility of setting the width and/or height of the plots, using the figurewidth/figureheight keys for tikz_save:
tikz_save('someplot.tex', ...
8
votes
Python and TeX, pythontex or hybrid-latex recommended?
Well, eventhough the question is borderline opinion-based, I will give you rundown of my knowledge and experience.
First off, that is to say, I have experience with PythonTeX and never used hybrid-...
8
votes
Accepted
where is the Python icon in fontawesome
Use fontawesome5 instead of fontawesome.
Interestingly the fontawesome5 does not list the icons in the manual. But
\documentclass[letterpaper, 11pt, onecolumn]{article}
\usepackage{fontawesome5}
...
7
votes
How to run PythonTeX?
G. Poore is correct but he is missing something nevertheless.
When you open cmd prompt and type pythontex [name_of_tex_file] make sure that you are doing this operation in the directory where the ...
7
votes
how to print a data frame from pandas using pylatex
It might be noted, that pandas DataFrames already have a quite powerful to_latex method. Another approach that makes use of this method and does not reinvent the wheel would be:
import numpy as np
...
7
votes
Accepted
In minted, how to reduce the blank space between the line number and the code?
The autogobble option is the way to go. Here is the excerpt from the manual:
Remove (gobble) all common leading whitespace from code. Essentially a version of gobble that automatically determines ...
7
votes
Lstlisting Python
You can use lmodern with the lighttt option.
\documentclass{article}
\usepackage[lighttt]{lmodern}
\usepackage{listings}
\lstset{basicstyle=\ttfamily, keywordstyle=\bfseries}
\begin{document}
\...
7
votes
Accepted
Jupyter In and Out Style
Jupyter Lab has an Export Notebook to LaTeX option in the File menu:
This creates a standalone file with a very large preamble (with color definitions, box drawing settings, etc), and the Notebook ...
7
votes
Accepted
How to use deletekeywords to remove keywords from lstlisting?
The file lstlang1.sty (use the command kpsewhich lstlang1.sty to find it) contains the definition of the Python languages for listings. The keywords are defined in two groups:
morekeywords={access, ...
7
votes
Accepted
is there a python module for parsing LaTeX?
Please see if the LatexWalker class of pylatexenc can help:
from pylatexenc.latexwalker import LatexWalker
w = LatexWalker(r"\foo{this is \emph{really} the foo argument}")
(nodelist, pos, ...
7
votes
Accepted
LaTeX is not letting me add spaces in strings in lstlistings
This is not a bug, it is a feature! With your current settings, spaces inside a string will be visualized using this "bucket", as you call it. This can be especially helpful if you have more ...
6
votes
How to highlight Python syntax in LaTeX Listings \lstinputlistings command
Some while ago I have modified the existing python language-definition from the listings-package:
Listings: syntax for literate
You might find something useful. If I remember correctly, one can put ...
6
votes
Adding additional blank lines in Minted
Minted 2.1 has a stripnl option, you can set to false:
\begin{minted}[stripnl=false]{bash}
exit
\end{minted}
6
votes
Accepted
Python colormaps in Tikz
Here is an approximation using a hsb gradient for the first part and using the red color in the middle of the second part.
\documentclass[tikz]{standalone}
\definecolor{left}{RGB}{255,255,205}
\...
6
votes
Accepted
Make >>> a keyword in listings enviorment
You can add >>> with morekeywords but you also have to change > to be a letter with the alsoletter option for it to work:
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
...
6
votes
Accepted
Problem using python package with MiKTeX
If pdflatex can't write the py file then the most probable cause is some protection software that denies it the rights. There has been some reports that e.g. bitdefender does this: pdflatex won't ...
6
votes
Accepted
Lstlisting Python
You need a font that has a bold version of the typewriter family. If you look in the .log file you will find:
LaTeX Font Info: Font shape `OT1/cmtt/bx/n' in size <10> not available
(Font) ...
6
votes
How to create this beautiful Python highlighting in LaTeX with listings?
The style you want looks very much like the one found in the overleaf guide: https://www.overleaf.com/learn/latex/code_listing
\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
python × 437pythontex × 47
listings × 42
minted × 42
pdftex × 31
code × 25
matplotlib × 23
tikz-pgf × 22
pylatex × 22
pygments × 14
pgfplots × 13
tables × 12
errors × 12
color × 12
pdf × 12
beamer × 11
xetex × 11
compiling × 10
windows × 10
fonts × 9
packages × 9
miktex × 8
texstudio × 8
scripts × 8
luatex × 7