3

I'd like to use code highlighting for LaTeX in my sectional titles. In the titles themselves it works, but the it does not in the Table of Contents. Here a minimal example:

\documentclass{scrartcl}
\usepackage{minted}

\begin{document}

\tableofcontents

\section{\mintinline{python}{print ``this''}}
\mintinline{python}{print ``this''}

\section{\mintinline{latex}{\documentclass[]{}}}
\mintinline{latex}{\documentclass[]{}}

\end{document}

and the corresponding output:

enter image description here

The complete error from the .toc file reads:

\GenericError { }{LaTeX Error: Can be used only in preamble}{See the LaTeX manual or LaTeX Companion for explanation.}{Your command was ignored.\MessageBreak Type I <command> <return> to replace it with another command,\MessageBreak or <return> to continue without it.

Any ideas how to allow \mintedinline in the ToC entries?

1 Answer 1

4

You have either to robustify the \minline command or to use \protect in the section command. However, this does not prevent breaking into parts concerning the \GenericError issue. You should use the optional argument of \section and use explicit \protect for such commands like \documentclass, otherwise \mintinline will print \documentclass directly to the ToC, resulting in two documentclass statements in the file.

However, I doubt the usefulness of having this text in section titles and ToC!

\documentclass [A4]{scrartcl}
\usepackage{minted}
\usepackage{etoolbox}

\robustify\mintinline

\begin{document}

\tableofcontents

\section{\protect\mintinline{python}{print ``this''}}
\mintinline{python}{print ``this''}

\section[\mintinline{latex}{\protect\documentclass[]{}}]{\mintinline{latex}{\documentclass[]{}}}
\mintinline{latex}{\documentclass[]{}}



\end{document}

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .