Mostrar macros matemáticas y dar como resultado una tabla
Estoy escribiendo documentación sobre algunas macros que he inventado. (Sé que probablemente también habrá comentarios sobre las macros, pero eso no es lo que estoy preguntando). Idealmente, me gustaría hacer una especie de tabla donde cada fila enumere la macro en sí, el resultado y un comentario . Aquí hay un MWE que muestra lo que estoy haciendo actualmente y lo que me gustaría hacer:
\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}

Respuestas
No tan diferente a la propuesta de Ulrich, pero con una codificación más simple. La macro tiene una * -variante para que el material se imprima en modo matemático.
\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}

El primer argumento se absorbe literalmente, pero luego se vuelve a escanear para usarlo en la segunda columna, posiblemente entre $
caracteres.
He editado mi ejemplo y, siguiendo el ejemplo de egreg, agregué un argumento de asterisco:
Con \ExampleRow*{...
el código se ejecuta en modo matemático.
Con \ExampleRow{...
el código solo se ejecuta en modo matemático si el propio código contiene directivas para cambiar al modo matemático.
Sin embargo, a diferencia de egreg, no uso \tl_rescan:nn
, pero sigo usando \scantokens
, porque cuando uso \scantokens
, cosas como \verb|...|
, que cambian al modo literal, también funcionan. Con \tl_rescan:nn
tales cosas no funcionan.
Las diferencias sutiles pero cruciales entre \scantokens
y \tl_rescan:nn
se tratan en las preguntas
¿Cuál es la diferencia esencial / crucial entre \ scantokens / \ tex_scantokens: D y \ tl_rescan: nn?
y
expl3 - ¿cómo se puede pasar el contenido de una variable de lista de tokens como argumento a otra "función"?
Como xparse está en uso, probablemente pueda tener \ExampleRow
- leer un argumento de tipo v (literalmente),
- imprimirlo con
\verbatim@font
efecto para mostrar la codificación - alimentarlo
\scantokens
, anidado$
y seguido por%
(\scantokens
inserta\endlinechar
...) para mostrar el resultado.
\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}

¿Quizás algo como lo siguiente? (esta vez sin espacios adicionales después de los nombres de las macros)
\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}
Solución anterior (los nombres de las macros irán seguidos de espacios)
Tal vez algo como
\newcommand\ExampleRow[2]{\texttt{\detokenize{#1}} & $#1$ & #2 \\}
es suficiente para tu propósito?
\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}
