gpoore / minted

minted is a LaTeX package that provides syntax highlighting using the Pygments library. Highlighted source code can be customized using fancyvrb.
1.74k stars 126 forks source link

`\mintinline{cpp}{#include}` in section title results in error #362

Closed zwz closed 1 year ago

zwz commented 1 year ago

Here is a MWE

\documentclass{article}  
\usepackage{minted}
\begin{document}
\section{... \mintinline{cpp}{#include} ...}             %% ! Illegal parameter number in definition of \reserved@a.
Normal text \mintinline{cpp}{#include} works.
\end{document}
gpoore commented 1 year ago

As mentioned in the documentation, \mintinline cannot be used inside other commands when the # character is used.

A saved box is probably the best solution:

\newsavebox\mybox
\begin{lrbox}{\mybox}
\mintinline{cpp}{#include}
\end{lrbox}

\section{Text \usebox{\mybox}}
zwz commented 1 year ago

Good to know this. I have to read the document again.

zwz commented 1 year ago

@gpoore I find I have to deal with font size in saved box, which is quite a burden. Inspired by https://tex.stackexchange.com/questions/249883/temporarily-changing-catcode-of something like

\catcode`#=11
\section{
\mintinline{cpp}{#include}
}
...

also works.

Just share it here, in case it helps improve this package.

muzimuzhi commented 1 year ago

@zwz Try my experimental \EscMintInlin, which works similar to \EscVerb from fvextra package, in https://github.com/gpoore/minted/issues/288#issuecomment-1501540637.

And you may want to restrict the scope of catcode changes locally, mostly in a group, like {\catcode#=11 \section{...}}.

zwz commented 1 year ago

@muzimuzhi It is great!

Yes, I put catcode in a group. But this trick does not work for %, which bothers me for a while before I see your comment and \EscMintInlin.

muzimuzhi commented 1 year ago

But this trick does not work for %

You need sth like \catcode\%=11`.

Update: \catcode`\%=11

gpoore commented 1 year ago

In the next version of minted, I can add a note in the documentation about catcode changes as an alternative to saving boxes, and can also look into adding a new variant of \mintinline based on fvextra's \EscVerb.

zwz commented 1 year ago

@muzimuzhi No, it results in error.

%% I also tried 
%\catcode`\%=11
\catcode%=11`
\section{
\mintinline{cpp}{3 % 2}
}
muzimuzhi commented 1 year ago

The error similar to

Runaway argument?
{\contentsline {section}{\numberline {2} \FVExtraRobustCommand \RobustMintInline \ETC.
! File ended while scanning use of \@writefile.
<inserted text> 
                \par 
l.6 \begin{document}

is caused by writing a direct % to .aux file. One further trick is to hide the percent char in a protected macro, but then such macro should be provided before begin document, otherwise you'll get error "Undefined Control Sequence xxx" when .aux is read in at that point. This would be inconvenient if you have many \mintinline containing % in sectioning titles.

Or if it's ok to typeset plain code in toc and headers, {\catcode`\%=11 \section[\EscVerb{3 \%\ 2}]{\mintinline{cpp}{3 % 2}}}. Right now I don't have the time to find why the space after \% must be escaped to be not gobbled in toc.

\documentclass{article}
\usepackage{minted}

{\catcode`\%=11
\protected\gdef\mymint{\mintinline{cpp}{3 % 2}}
}

\begin{document}
\tableofcontents

\section{title}
\section{\mymint}

{\catcode`\%=11
\section[\EscVerb{3 \% 2}]{\mintinline{cpp}{3 % 2}}
\section[\EscVerb{3 \%\ 2}]{\mintinline{cpp}{3 % 2}}
}
\end{document}

image

zwz commented 1 year ago

Thank you for looking into the error. It helps a lot.

zwz commented 1 year ago

@gpoore I read the document those days. It says: "Curly braces are required when \mintinline is used in a movable argument, such as in a \section." I tried something like \section{Text \mintinline{cpp}|if(0){printf("never");}|} and it works. So I am wondering whether {} are not required anymore?

gpoore commented 1 year ago

@zwz There are some cases where you can use something besides {}, but that typically won't work correctly if you need \tableofcontents.