在表格中显示数学宏和结果

在表格中显示数学宏和结果

我正在写一些关于我编写的一些宏的文档。(我知道可能也会有关于宏的评论,但这不是我问的。)理想情况下,我想制作一种表格,其中每行列出宏本身、结果和评论。这是一个 MWE,显示了我目前正在做的事情以及我想要做的事情:

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{xparse}
\usepackage{lmodern}
\usepackage{listings}
\usepackage[most]{tcolorbox}

\DeclareTCBListing{macrobox}{s G{} }{IfBooleanTF={#1}{}{listing side text},title=#2,
  listing options={style=tcblatex,commentstyle=\color{red!70!black}}
}

% Funny macro - don't worry about this part
\NewDocumentCommand{\TnidxRoot}{m m m m m m m}{%
  {#1}_{1} #2 %
  \IfBooleanF{#5}{{#1}_{2} #2} % 
  \IfBooleanTF{#6}{ % 
    {#1}_{3} % 3D
    \IfBooleanT{#7}{#2 {#1}_{4} } % 4D
  }{ % 
    \IfBooleanTF{#3}{\cdots}{\dots} #2 {#1}_{#4} %
  }%
}
% Funny macro - don't worry about this part
\NewDocumentCommand{\Tnsz}{s s t! G{n} O{d}}{%
  \TnidxRoot{#4}{\times}{\BooleanTrue}{#5}{#3}{#1}{#2}%
}

\begin{document}

What I currently do is this:
\begin{macrobox}{Size commands}
$\Tnsz$          \\ % Default
$\Tnsz!$         \\ % Compact, no 2nd index
$\Tnsz{m}$        % Change main letter
\end{macrobox}


But I'd like a nice macro to generate rows of the following table, where the macro for row 1 might be something like \verb|\ExampleRow{\Tnsz}{Default}|.

\begin{tabular}{|l|l|l|}
  \hline
  \LaTeX\ Command & Result & Description \\ \hline
  \verb|\Tnsz| & $\Tnsz$ & Default \\ \hline
  \verb|\Tnsz!| & $\Tnsz!$ & Compact, no second index \\ \hline
  \verb|\Tnsz{m}| & $\Tnsz{m}$ & Change main letter \\ \hline
\end{tabular}

\end{document}

MWE 输出

答案1

与 Ulrich 的提议并无太大区别,但编码更简单。宏有一个 *-variant,用于在数学模式下打印材料。

\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs}
%\usepackage{xparse} % uncomment if using LaTeX prior to 2020-10-01

\ExplSyntaxOn

\NewDocumentCommand{\ExampleRow}{svm}
 {
  \texttt{#2} &
  \IfBooleanT{#1}{$} \tl_rescan:nn { } { #2 } \IfBooleanT{#1}{$} &
  #3 \\
 }

\ExplSyntaxOff

\begin{document}

\begin{tabular}{lll}
\toprule
\ExampleRow{\fbox{a}}{Make a framed box}
\ExampleRow{\framebox[2cm][l]{a}}{Make a framed box}
\ExampleRow*{\sin}{Sine function}
\ExampleRow*{\xrightarrow{f}}{Extendable arrow}
\ExampleRow*{\xrightarrow[g]{}}{Extendable arrow}
\bottomrule
\end{tabular}

\end{document}

在此处输入图片描述

第一个参数被逐字吸收,但随后被重新扫描以便用于第二列,可能在$字符之间。

答案2

我编辑了我的示例,并按照 egreg 的示例添加了一个星号参数:
代码\ExampleRow*{...在数学模式下执行。
\ExampleRow{...仅当代码本身包含切换到数学模式的指令时,代码才会在数学模式下执行。

但是与 egreg 不同的是,我不使用\tl_rescan:nn,而是仍然使用\scantokens,因为当使用 时,切换到逐字模式之\scantokens类的东西也可以工作。使用这些东西不起作用。\verb|...|\tl_rescan:nn

问题讨论了\scantokens和之间的微妙但关键的区别\tl_rescan:nn
\scantokens/\tex_scantokens:D 和 \tl_rescan:nn 之间本质/关键区别是什么?

expl3 - 如何将标记列表变量的内容作为参数传递给另一个“函数”?


由于正在使用 xparse,你可能需要\ExampleRow

  • 读取类型 v 的参数(逐字读取),
  • 打印出来\verbatim@font以显示编码
  • 将其喂给\scantokens,嵌套$并尾随%\scantokens插入\endlinechar...)以显示结果。

 

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{xparse}
\usepackage{lmodern}
\usepackage{listings}
\usepackage[most]{tcolorbox}

\DeclareTCBListing{macrobox}{s G{} }{IfBooleanTF={#1}{}{listing side text},title=#2,
  listing options={style=tcblatex,commentstyle=\color{red!70!black}}
}

% Funny macro - don't worry about this part
\NewDocumentCommand{\TnidxRoot}{m m m m m m m}{%
  {#1}_{1} #2 %
  \IfBooleanF{#5}{{#1}_{2} #2} % 
  \IfBooleanTF{#6}{ % 
    {#1}_{3} % 3D
    \IfBooleanT{#7}{#2 {#1}_{4} } % 4D
  }{ % 
    \IfBooleanTF{#3}{\cdots}{\dots} #2 {#1}_{#4} %
  }%
}
% Funny macro - don't worry about this part
\NewDocumentCommand{\Tnsz}{s s t! G{n} O{d}}{%
  \TnidxRoot{#4}{\times}{\BooleanTrue}{#5}{#3}{#1}{#2}%
}

%--------------------------------------------------------------------------
% This is what I would probably do:
%--------------------------------------------------------------------------
\begingroup
\makeatletter
% Let's use ^^A instead of %.
% Make % other so it can be "fed" to \scantokens to handle \scantokens'
% insertion of \endlinechar;
\catcode`\^^A=14\relax
\catcode`\%=12\relax
\@firstofone{^^A
  \endgroup
  \NewDocumentCommand\ExampleRow{svm}{^^A
    {\verbatim@font#2}&^^A
    {\IfBooleanT{#1}{$}\scantokens{#2%}\IfBooleanT{#1}{$}}&^^A
    {#3}\\\hline^^A
  }^^A
}%
%--------------------------------------------------------------------------

\begin{document}

What I currently do is this:
\begin{macrobox}{Size commands}
$\Tnsz$          \\ % Default
$\Tnsz!$         \\ % Compact, no 2nd index
$\Tnsz{m}$        % Change main letter
\end{macrobox}


But I'd like a nice macro to generate rows of the following table, where the macro for row 1 might be something like \verb|\ExampleRow{\Tnsz}{Default}|.

\begin{tabular}{|l|l|l|}
  \hline
  \LaTeX\ Command & Result & Description \\ \hline
  \verb|\Tnsz| & $\Tnsz$ & Default \\ \hline
  \verb|\Tnsz!| & $\Tnsz!$ & Compact, no second index \\ \hline
  \verb|\Tnsz{m}| & $\Tnsz{m}$ & Change main letter \\ \hline
  \verb+\verb|verbatim stuff|+ & \verb|verbatim stuff| & verbatim material \\ \hline
\end{tabular}

\bigskip

Here it is:

\bigskip

\begin{tabular}{|l|l|l|}
  \hline
  \LaTeX\ Command & Result & Description \\ \hline
  \ExampleRow*{\Tnsz}{Default}%
  \ExampleRow*{\Tnsz!}{Compact, no second index}%
  \ExampleRow*{\Tnsz{m}}{Change main letter}%
  \ExampleRow{\verb|verbatim stuff|}{verbatim material}%
\end{tabular}

\end{document}

在此处输入图片描述

答案3

也许像下面这样?(这次宏名后没有多余的空格)

\makeatletter
\newcommand\strings[1]{\@tfor\sss:=#1\do{\expandafter\string\sss}}
\makeatother
\newcommand\ExampleRow[2]{\texttt{\strings{#1}} & $#1$ & #2 \\}

在此处输入图片描述

\documentclass{article}
\makeatletter
\newcommand\strings[1]{\@tfor\sss:=#1\do{\expandafter\string\sss}}
\makeatother
\newcommand\ExampleRow[2]{\texttt{\strings{#1}} & $#1$ & #2 \\}
\begin{document}
\begin{tabular}{lll}
  \ExampleRow{\times!\times}{binary product}
  \ExampleRow{\prod}{generic product}
\end{tabular}
\end{document}

旧解决方案(宏名后面会有空格)

也许类似

\newcommand\ExampleRow[2]{\texttt{\detokenize{#1}} & $#1$ & #2 \\}

是否足以达到你的目的?

\documentclass{article}
\newcommand\ExampleRow[2]{\texttt{\detokenize{#1}} & $#1$ & #2 \\}
\begin{document}
\begin{tabular}{lll}
  \ExampleRow{\times}{binary product}
  \ExampleRow{\prod}{generic product}
\end{tabular}
\end{document}

在此处输入图片描述

相关内容