![\mintinline 中的宏用法](https://linux22.com/image/291686/%5Cmintinline%20%E4%B8%AD%E7%9A%84%E5%AE%8F%E7%94%A8%E6%B3%95.png)
我尝试在里面使用宏\mintinline
。结果如下:
\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}
该宏\mintinline
是在版本 v2 alpha 2.0 中引入的(目前为 alpha 3,Github 链接,并且突出显示与 Latex/Minted 一致的源代码?)
一定有没有“更好”的方法来做到这一点?:-) 一些解决方案也可以允许语法突出显示按预期运行......(这将是一个难题,因为 \mintinline 占用了一个参数,如下所示:\mintinline{Python}{code goes here}
)\
我在用铸造 v2.0 alpha 3。可从以下网址下载https://github.com/gpoore/minted/tree/master/source
答案1
让我重新格式化它,使其可以阅读。
\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}
现在我将换成\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
\EXPANDAFTER\mintinline{\ab}
最后一个\EXPANDAFTER
现在尝试扩展 之后的标记\mintinline
,这很容易:{
无法扩展。因此所有标记的\expandafter
标记都消失了,我们只剩下
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\mintinline{\ab}
重复前面的步骤:
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\mintinline{\ab}
最后一个扩展(一次)\mintinline
,根据\mintinline
包中的定义,在输入流中留下以下内容:
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\@protected@testopt\mintinline\\mintinline{}{\ab}
注意,这\\mintinline
只是一个 token。我们再玩一遍:
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\EXPANDAFTER\expandafter\EXPANDAFTER\expandafter
\@protected@testopt\mintinline\\mintinline{}{\ab}
这里\@protected@testop
展开一次;它在 LaTeX 内核中的定义是
% latex.ltx, line 619:
\def\@protected@testopt#1{%%
\ifx\protect\@typeset@protect
\expandafter\@testopt
\else
\@x@protect#1%
\fi}
因此,在删除已经使用的\expandafter
标记并替换扩展的宏之后,输入流具有
\expandafter\expandafter\expandafter\expandafter
\ifx\protect\@typeset@protect\expandafter\@testopt
\else\@x@protect\mintinline\fi\\mintinline{}{\ab}
第一个\expandafter
展开了第三个,第三个又展开了\ifx
;因为我们处于“正常”位置,所以测试是正确的,所以我们剩下
\expandafter\expandafter\expandafter\@testopt
\else\@x@protect\mintinline\fi\\mintinline{}{\ab}
第一个\expandafter
扩展了第三个,第三个又扩展了\else
;这很简单:匹配之前的所有内容\fi
都消失了:
\expandafter\@testopt\\mintinline{}{\ab}
现在\\mintinline
已经扩展;但是它的定义是
> \\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 }.
根据 LaTeX 如何处理使用可选参数定义的命令。由于\\mintinline
想要之后[
,错误
! Use of \\mintinline doesn't match its definition.
\mintinline ...@testopt \mintinline \\mintinline {
}
已发出。
有没有更好的方法呢?有的,例如记住以下语法\mintinline
:
\mintinline[<options>]{<language>}{<verbatim material>}
那串 33 个\expandafter
标记简直是荒谬的:相反,这样做可以:
\begingroup\def\temp{\endgroup\mintinline{c}}% use your preferred language
\expandafter\temp\expandafter{\ab}
但是,如果\ab
被认为包含特殊字符,它们可能不会得到特殊处理,因为它们已经被标记化。
答案2
根据minted
文档,其语法为\mintinline
:
\mintinline[<options>]{<language>}<delim><code><delim>
看来你缺少一个论点——语言规范:
\documentclass{article}
\newcommand{\ab}{expanded stuff}
\newcommand{\pythoninline}{\mintinline{python}}
\usepackage{minted}
\begin{document}
\expandafter\pythoninline\expandafter{\ab}
\end{document}