10

Since Python 3.6, released Dec 2016, there is new f-string formatting, where, inside your string you can put python code to be evaluated in {}s. It's fantastic but TeX packages for code listings with syntax highlighting don't know about it, or do they?

MWE:

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}

\definecolor{cCodeString}{RGB}{42,0.0,255}
\definecolor{cCodeKeywordstyle}{RGB}{150,0,150}
\definecolor{cCodeComment}{RGB}{63,127,95}

\lstset{language=python,
    columns=fullflexible,
    basicstyle=\small\ttfamily,
    keywordstyle=\color{cCodeKeywordstyle}\bfseries,
    commentstyle=\color{cCodeComment},
    stringstyle=\color{cCodeString},
}

\begin{document}

\begin{lstlisting}
name = "Ann"
print(f"{name}, you have {len(name):4} characters in your name.")
\end{lstlisting}

\end{document}

enter image description here

It would be so good if the stuff inside the {} looked like code because the interpreter sees it as code.

  • 2
    Because your question was sent to the close queue as unclear I decided to edit it to make the connection to LaTeX clearer: I assume you want to ask about syntax highlighting in packages like listings (one tag you used) or minted. If that is not the case or I got anything else wrong, please feel free to roll back the edit, but please make sure to reformulate your question afterwards to avoid confusion. On first glance the question could have been read as asking about Python and not about LaTeX at all. – moewe Dec 19 '19 at 7:02
  • 3
    Note that the edit by @moewe is not functional Python code – this question refers to "f-string formatting", so for this to actually work, the string needs to be prefixed by f, as in print(f"{name}"), – Schmuddi Dec 19 '19 at 8:28
14

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}{python}
  name = "Ann"
  print(f"{name}, you have {len(name)} characters in your name.")
\end{minted}
\end{document}

which renders as:

enter image description here

Note, however, that while LaTeX's minted package detects the first variable ({name}), it fails to detect the second one ({len(name)}).

This is actually a problem not of the minted package but of the Pygments Python lexer, as the pygmentize CLI renders it identically. (The minted package uses Pygments as its backend.)

I haven't found any solution to this problem yet, but have created an issue at the Pygment's GitHub repo.

  • Thank you for your efforts. This system wouldn't let me add "latex" as a tag, I guess because the forum is about latex? – Marilyn Davis Dec 19 '19 at 18:50
  • Interesting is that Jupyter Notebooks gets it right. – Marilyn Davis Dec 19 '19 at 18:50
  • 1
    Yeah, but they must surely use a more robust syntax highlighting engine. For instance, as far as I know, neither listings nor minted (Pygments) care if a symbol is actually a variable. If the variable name doesn't exist the result is exactly the same. Jupyter does care ---needs to care--- for that. – NVaughan Dec 19 '19 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.