4

I tried to use a macro inside \mintinline. This is the result:

\documentclass{article}

\newcommand\ab{expanded stuff}

\usepackage{minted}

\begin{document}

\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\mintinline{\ab}

\end{document}

The macro \mintinline is introduced in version v2 alpha 2.0 (currently alpha 3, Github link, and also Highlighted source code inline with Latex/Minted? )

Surely there's a "better" way to do this? :-) Some solution that would also allow syntax highlighting to function as intended... (This will be a tough one because \mintinline takes up an argument, like this: \mintinline{Python}{code goes here})\

I am using minted v2.0 alpha 3. Can be downloaded at https://github.com/gpoore/minted/tree/master/source

7
  • Why not just \expandafter\mintinline\expandafter{\ab}?
    – Werner
    May 30, 2014 at 23:44
  • @Werner I get an error Paragraph ended before \minted@inline@i was complete. from that ???
    – 1010011010
    May 30, 2014 at 23:46
  • The example doesn't compile, \mintinline is not defined.
    – percusse
    May 30, 2014 at 23:53
  • @percusse I'm running TeXworks 0.4.5 r1280 and minted v2.0 alpha 3, and it does compile here. :-/
    – 1010011010
    May 30, 2014 at 23:54
  • 1
    I don't have the version of minted you mention. Provide a link so we can test.
    – Werner
    May 31, 2014 at 4:34

2 Answers 2

9

Let me reformat it to be readable.

\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\expandafter\mintinline{\ab}

Now I'll change into \EXPANDAFTER those that are under the effect of the first one, just to distinguish them

\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\mintinline{\ab}

The last \EXPANDAFTER now tries to expand the token after \mintinline, which is an easy job: a { can't be expanded. So all marked \expandafter tokens disappear and we're left with

\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\mintinline{\ab}

Repeat as before:

\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\mintinline{\ab}

The last one expands (once) \mintinline, which, according to the definition of \mintinline in the package, leaves the following in the input stream:

\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\@protected@testopt\mintinline\\mintinline{}{\ab}

Note that \\mintinline is just one token. Let's play again:

\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\@protected@testopt\mintinline\\mintinline{}{\ab}

Here \@protected@testop is expanded once; its definition in the LaTeX kernel is

% latex.ltx, line 619:
\def\@protected@testopt#1{%%
  \ifx\protect\@typeset@protect
    \expandafter\@testopt
  \else
    \@x@protect#1%
  \fi}

so, after removing the already used \expandafter tokens and replacing the expanded macro, the input stream has

\expandafter\expandafter\expandafter\expandafter
\ifx\protect\@typeset@protect\expandafter\@testopt
\else\@x@protect\mintinline\fi\\mintinline{}{\ab}

The first \expandafter expands the third, which in turn expands \ifx; since we are in a “normal” place, the test is true, so we're left with

\expandafter\expandafter\expandafter\@testopt
\else\@x@protect\mintinline\fi\\mintinline{}{\ab}

The first \expandafter expands the third, which expands \else; this is easy: everything up to and including the matching \fi disappears:

\expandafter\@testopt\\mintinline{}{\ab}

Now \\mintinline is expanded; but its definition is

> \\mintinline=\long macro:
[#1]#2->\minted@resetoptcmd \setkeys {minted@optcmd}{#1}\def \minted@lang {#2}
\begingroup \let \do \@makeother \dospecials \catcode `\{=1 \catcode `\}=2 
\catcode `\ =\active \@ifnextchar \bgroup {\minted@inline@iii }{\catcode 
`\{=12\catcode `\}=12 \minted@inline@i }.

according to how LaTeX handles commands defined with an optional argument. Since \\mintinline wants a [ after it, the error

! Use of \\mintinline doesn't match its definition.
\mintinline ...@testopt \mintinline \\mintinline {
                                                  }

is issued.

Are there better ways to do it? Yes, for example remembering the syntax of \mintinline:

\mintinline[<options>]{<language>}{<verbatim material>}

That string of 33 \expandafter tokens is simply absurd: this, instead, will do:

\begingroup\def\temp{\endgroup\mintinline{c}}% use your preferred language
\expandafter\temp\expandafter{\ab}

However, if \ab is thought to contain special characters, they might not get treated specially, because they have already been tokenized.

1
  • 3
    Now I am really expanded ;-) May 31, 2014 at 9:58
5

According to the minted documentation, the syntax for \mintinline is:

\mintinline[<options>]{<language>}<delim><code><delim>

It seems you're short an argument - the language specification:

enter image description here

\documentclass{article}

\newcommand{\ab}{expanded stuff}
\newcommand{\pythoninline}{\mintinline{python}}

\usepackage{minted}

\begin{document}

\expandafter\pythoninline\expandafter{\ab}

\end{document}

You must log in to answer this question.

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