Unless I misunderstood the comments, what you would like is this:
\documentclass{article}
\usepackage{minted}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand \imp { o }
{
\IfValueTF {#1}
{ \mintinline[#1]{python} }
{ \mintinline{python} }
}
\ExplSyntaxOff
\begin{document}
\imp{'ONE TWO'}
\mintinline{python}{'ONE TWO'} % 'ONE TWO'
\imp{\} % no error
\mintinline{python}{\} % \
\end{document}
I added \ExplSyntaxOn
and \ExplSyntaxOff
, otherwise your formatting would add unwanted spaces.
Note that the following definition would work as well; however, I think it's a bit more difficult to understand for very little gain:
\NewDocumentCommand \imp { o }
{
\IfValueTF {#1}
{ \mintinline[#1] }
{ \mintinline }
{python}
}
The reason why your macro didn't work is that commands that grab verbatim text (here, \mintinline
) grab it with a special category code setup (for instance, spaces aren't treated like TeX normally does). But when your macro is called and grabs its #2
, TeX assigns each resulting character token a category code according to the current category code setup, which is not the setup \mintinline
would have done; and once assigned, the category codes stay firmly attached to their character tokens, unless you use very special commands such as \detokenize
.
Note that even \detokenize
couldn't help here: if you read a b
under TeX's normal category code setup (e.g., if it is grabbed as a macro argument), the result is a token list containing three character tokens:
a
with category code 11 (letter);
- a space token (character code 32, category code 10);
b
with category code 11.
The four spaces from the input have already coalesced into one space token. From the resulting token list, there is no way to recover the original number of spaces. The only way not to lose such information is to set up the category codes differently before the characters from the input file are turned into tokens (this is what \mintinline
, \verb
and similar commands do).
When characters from the input have been grabbed as part of a macro argument1 under the normal category code regime, it is already too late for verbatim
-like handling: the characters from the input have been tokenized, i.e., they have been turned into a list of tokens, each of which is either a control sequence token (e.g., \foobar
) or a character token (a pair consisting of a character code and a category code). So, the solution here is not to grab the input characters we want to be treated in a verbatim
-like fashion, and let \mintinline
do it herself with the proper category code setup for this task.
Footnote
- This includes environment arguments, since environments are implemented with macros.
\imp
?\mintinline
, namely{python}