我正在尝试找出一种方法来制作一个宏,在其中演示命令的输出,然后通过命令显示该命令lstinline
。我读到我需要转义特殊字符(如{
和}
),现在想知道是否有办法自动执行此操作。不确定这是否可行。
理想情况下,该\commandExample{\qty{1.2}{\meter}}
命令将产生Output:1.2 m - Code:\qty{1.2}{\meter}
梅威瑟:
\documentclass{report}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{siunitx}
\usepackage{listings}
\definecolor{lightgrey}{rgb}{0.9,0.9,0.9}
\definecolor{darkgreen}{rgb}{0,0.6,0}
\lstset{language=[LaTeX]TeX,
caption = {Missing Caption}
,label = {lst:missingLabel}
,basicstyle = \footnotesize\ttfamily %\footnotesize % \small\ttfamily
,frame = shadowbox%
,numbers = left%
,breaklines = true%
,keywordstyle = \color{darkgreen}%
,commentstyle = \color{red}%
,tabsize = 2%
,backgroundcolor = \color{lightgrey}%
%,texcsstyle = {*\bf\color{blue}}%
%,otherkeywords = $, \{, \}, \[, \]%
,morekeywords = {includegraphics }%
,moretexcs = {graphicspath}%
}
\NewDocumentCommand{\commandExample}{m}{%
Output:#1 - Code:\lstinline{#1}}
\begin{document}
\commandExample{begin}.
\unit{\meter}
%\commandExample{\unit{\meter}} % This line causes it to crash
\end{document}
答案1
我会逐字逐句地吸收这个论点并用\tl_rescan:nn
它来排版。
\documentclass{report}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{siunitx}
\usepackage{listings}
\definecolor{lightgrey}{rgb}{0.9,0.9,0.9}
\definecolor{darkgreen}{rgb}{0,0.6,0}
\lstset{language=[LaTeX]TeX,
caption = {Missing Caption}
,label = {lst:missingLabel}
,basicstyle = \footnotesize\ttfamily
,frame = shadowbox
,numbers = left
,breaklines = true
,keywordstyle = \color{darkgreen}
,commentstyle = \color{red}
,tabsize = 2
,backgroundcolor = \color{lightgrey}
,texcsstyle = {*\bfseries\color{blue}}
%,otherkeywords = {$, \{, \}, \[, \]}
,morekeywords = {includegraphics,unit}
,moretexcs = {graphicspath}%
}
\ExplSyntaxOn
\NewDocumentCommand{\commandExample}{v}
{
Output:~\tl_rescan:nn {} {#1} ~ - ~ Code:~\lstinline{#1}
}
\ExplSyntaxOff
\begin{document}
\commandExample{\unit{\meter}}
\commandExample{\textit{word}}
\end{document}
答案2
一般来说egreg 提供的代码比我的代码更受欢迎。
\mathescape=true
但是,通过在 内启用的数学模式机制\lstset
,以及允许\lstinline
通过 之间嵌套的东西跳转到 参数内的数学模式,在直接应用于 v 型参数$...$
时被破坏。\lstinline
启用 mathescape 后,如果将模式标记为 v-type-argument,该命令\lstinline{$x^2+y^2=2^2$}
会向您提供大量模式错误消息。! Undefined control sequence. \lst@arg ->$x
$x^2+y^2=2^2$
因此,如果在现实生活中您不需要 mathescape 功能,我建议使用 egreg 的代码。
如果在现实生活中您确实需要 mathescape 功能,请修改 egreg 的答案,以便\commandExample
在适当的类别代码制度下读取和标记其参数,或者尝试一种方法,将\scantokens
或\tl_rescan:nn
应用于传递给的标记\lstinline
。
一个小问题是:对于较新的 LaTeX 发行版,您的代码没有提供\unit
和 的任何定义\meter
。因此在下面的示例中,使用命令\si
和\metre
代替。
一个问题是: 的一个特性是,如果 本身也包含 ,则\lstinline
的参数\lstinline
通常不能嵌套在{
和之间。在这种情况下,参数需要嵌套在参数中未出现的字符之间。所以行不通,但你需要做一些类似的事情。(另一个技巧,在 egreg 的答案中完成,是在 verbatim-catcode-régime 下对 的参数进行标记,以便在任何情况下它都不包含类别 1 或类别 2。但-type-category-code-régime 破坏了 的一些功能- 至少是 mathescape 功能。)}
{
\lstinline{\unit{\meter}}
\lstinline|\unit{\meter}|
\lstinline
{
}
v
\lstinline
另一个问题是 TeX 期望 的参数\lstinline
在不同的类别代码机制下进行标记。因此\lstinline
会暂时更改类别代码机制,以从标记流中抓取属于其参数的标记。\lstinline
依赖于 TeX 需要从 .tex 输入文件中读取并在更改后的类别代码机制下对属于参数的标记进行标记,以便可以从标记流中抓取它们。因此\lstinline
只在用户级/顶层有效。\lstinline
在未改变类别代码机制下对参数进行标记后传入时不起作用,例如,当将 的调用\lstinline
放入其他宏的定义文本中时,这些宏会在未改变类别代码机制下抓取参数并将其传递给\lstinline
。
\commandExample
通过让宏本身在获取参数之前更改类别代码机制并将其传递给\lstinline
嵌套的,可以在一定程度上规避这个问题\scantokens{...%}
。
但由此又出现了另一个问题:LaTeX 2ε、expl3 和 xparse 都没有提供逐字参数类型来让您保存和传递逐字分隔符。
因此,在下面的示例中,\UDcollectverbarg
提供了切换到逐字 catcode 制度的命令,并抓取没有逐字分隔符和嵌套在逐字分隔符之间的参数,以便这些内容可以正确传递以进行进一步处理。
\makeatletter
%%======================Code for \UDcollectverbarg=============================
%% \UDcollectverbarg{<mandatory 1>}{<mandatory 2>}|<verbatim arg>|
%%
%% reads <verbatim arg> under verbatim-catcode-regime and delivers:
%%
%% <mandatory 1>{<mandatory 2>{<verbatim arg>}{|<verbatim arg>|}}
%%
%% Instead of verbatim-delimiter | the <verbatim arg> can be nested in braces.
%% You cannot use percent or spaces or horizontal tab as verbatim-delimiter.
%%
%% You can use <mandatory 1> for nesting calls to \UDcollectverbarg.
%% <mandatory 2> gets the <verbatim arg> twice: Once without verbatim-delimiters/braces,
%% once surrounded by verbatim-delimiters/braces.
%% Reason: When you feed it to \scantokens you don't need the verbatim-delimiters.
%% When you use it for writing to temporary files and reading back,
%% you need them.
%%=============================================================================
%% Check whether argument is empty:
%%=============================================================================
\@ifdefinable\UD@stopromannumeral{\chardef\UD@stopromannumeral=`\^^00}%
%%
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is empty>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
\newcommand\UD@CheckWhetherNull[1]{%
\romannumeral\expandafter\@secondoftwo\string{\expandafter
\@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
\@secondoftwo\string}\expandafter\@firstoftwo\expandafter{\expandafter
\@secondoftwo\string}\expandafter\UD@stopromannumeral\@secondoftwo}{%
\expandafter\UD@stopromannumeral\@firstoftwo}%
}%
%%=============================================================================
\newcommand\UDcollectverbarg[2]{%
\begingroup
\let\do\@makeother % <- this and the next line switch to
\dospecials % verbatim-category-code-régime.
\catcode`\{=1 % <- give opening curly brace the usual catcode so a
% curly-brace-balanced argument can be gathered in
% case of the first thing of the verbatimized-argument
% being a curly opening brace.
\catcode`\ =10 % <- give space and horizontal tab the usual catcode so \UD@collectverbarg
\catcode`\^^I=10 % cannot catch a space or a horizontal tab as its 4th undelimited argument.
% (Its 4th undelimited argument denotes the verbatim-
% syntax-delimiter in case of not gathering a
% curly-brace-nested argument.)
\catcode`\%=14 % <- make percent comment.
\kernel@ifnextchar\bgroup
{% seems a curly-brace-nested argument is to be caught:
\catcode`\}=2 % <- give closing curly brace the usual catcode also.
\UD@collectverbarg{#1}{#2}{}%
}{% seems an argument with verbatim-syntax-delimiter is to be caught:
\do\{% <- give opening curly brace the verbatim-catcode again.
\UD@collectverbarg{#1}{#2}%
}%
}%
\newcommand\UD@collectverbarg[3]{%
\do\ % <- Now that \UD@collectverbarg has the delimiter or
\do\^^I% emptiness in its 4th arg, give space and horizontal tab
% the verbatim-catcode again.
\do\^^M% <- Give the carriage-return-character the verbatim-catcode.
\do\%% <- Give the percent-character the verbatim-catcode.
\long\def\@tempb##1#3{%
\def\@tempb{##1}%
\UD@CheckWhetherNull{#3}{%
\def\@tempc{{##1}}%
}{%
\def\@tempc{#3##1#3}%
}%
\@onelevel@sanitize\@tempb % <- Turn characters into their "12/other"-pendants.
% This may be important with things like the
% inputenc-package which may make characters
% active/which give them catcode 13(active).
\@onelevel@sanitize\@tempc
\expandafter\expandafter\expandafter\UD@@collectverbarg% <- this "spits out the result.
\expandafter\expandafter\expandafter{%
\expandafter\@tempb\expandafter}%
\expandafter{\@tempc}{#1}{#2}%
}%
\@tempb
}%
\newcommand\UD@@collectverbarg[4]{%
\endgroup
#3{#4{#1}{#2}}%
}%
%%================= End of code for \UDcollectverbarg =========================
\makeatother
%%
\documentclass{report}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{siunitx}
\usepackage{listings}
\definecolor{lightgrey}{rgb}{0.9,0.9,0.9}
\definecolor{darkgreen}{rgb}{0,0.6,0}
\lstset{language=[LaTeX]TeX,
% !!! Let's enable the mathescape-feature to test if it works:
mathescape=true,
%
caption = {Missing Caption}
,label = {lst:missingLabel}
,basicstyle = \footnotesize\ttfamily %\footnotesize % \small\ttfamily
,frame = shadowbox%
,numbers = left%
,breaklines = true%
,keywordstyle = \color{darkgreen}%
,commentstyle = \color{red}%
,tabsize = 2%
,backgroundcolor = \color{lightgrey}%
%,texcsstyle = {*\bf\color{blue}}%
%,otherkeywords = $, \{, \}, \[, \]%
,morekeywords = {includegraphics }%
,moretexcs = {graphicspath}%
}
\makeatletter
\NewDocumentCommand{\commandExample}{}{%
\UDcollectverbarg{\@firstofone}{\@commandExample}%
}%
\begingroup
\catcode`\X=14 %
\catcode`\%=12 X
\csname @firstofone\endcsname{X
\endgroup
\NewDocumentCommand{\@commandExample}{mm}{X
\scantokens{Output: #1 - Code: \lstinline#2%}X
}X
}%
\makeatother
\begin{document}
\verb:\commandExample{begin}: yields:
\commandExample{begin}
\medskip\hrule\medskip
\noindent Both with \verb|\lstinline| and thus with this variant of \verb|\commandExample| you can't have
\verb|{| within arguments that are nested between \verb|{...}|. Use some verbatim-delimiter instead:
\medskip
\verb:\commandExample|\si{\metre}|: yields:
\commandExample|\si{\metre}|
\medskip\hrule\medskip
\noindent \verb|mathescape|-thingies in the code seem to work, too:
\medskip
\verb:\commandExample|$x^2+y^2=z^2$|: yields:
\commandExample|$x^2+y^2=z^2$|
\medskip\hrule\medskip
\verb:\commandExample{$x^2+y^2=z^2$}: yields:
\commandExample{$x^2+y^2=z^2$}
\end{document}