Hot answers tagged

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}
CodingYourLife's user avatar
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 ...
ebosi's user avatar
  • 11.5k
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 = { # ...
Skillmon's user avatar
  • 57.8k
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 ...
Torbjørn T.'s user avatar
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}{...
NVaughan's user avatar
  • 8,085
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} \...
Sigur's user avatar
  • 37k
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 ...
Michael Palmer's user avatar
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.
nicky's user avatar
  • 131
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 ...
Ulrike Fischer's user avatar
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). ...
yo''s user avatar
  • 50.9k
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 ...
David Carlisle's user avatar
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? ...
user202729's user avatar
  • 6,364
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', ...
Torbjørn T.'s user avatar
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-...
Tomáš Kruliš's user avatar
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} ...
daleif's user avatar
  • 53.1k
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 ...
amr_abdo's user avatar
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 ...
mcocdawc's user avatar
  • 323
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 ...
yudai-nkt's user avatar
  • 921
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} \...
egreg's user avatar
  • 1.1m
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 ...
Marijn's user avatar
  • 36k
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, ...
Paul Gaborit's user avatar
  • 70.2k
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, ...
Matteo Gamboz's user avatar
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 ...
Οὖτις's user avatar
  • 2,877
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 ...
BadAtLaTeX's user avatar
  • 1,109
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}
maxdebayser's user avatar
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} \...
Paul Gaborit's user avatar
  • 70.2k
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} ...
cgnieder's user avatar
  • 66.3k
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 ...
Ulrike Fischer's user avatar
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) ...
Andrew Swann's user avatar
  • 94.9k
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} \...
DG''s user avatar
  • 21.5k

Only top scored, non community-wiki answers of a minimum length are eligible