Closed zwz closed 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}}
Good to know this. I have to read the document again.
@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.
@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{...}}
.
@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
.
But this trick does not work for
%
You need sth like .\catcode
\%=11`
Update: \catcode`\%=11
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
.
@muzimuzhi No, it results in error.
%% I also tried
%\catcode`\%=11
\catcode%=11`
\section{
\mintinline{cpp}{3 % 2}
}
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}
Thank you for looking into the error. It helps a lot.
@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?
@zwz There are some cases where you can use something besides {}
, but that typically won't work correctly if you need \tableofcontents
.
Here is a MWE