Mostra le macro matematiche e genera una tabella
Sto scrivendo della documentazione su alcune macro che ho creato. (So che probabilmente ci saranno anche commenti sulle macro, ma non è quello che sto chiedendo.) Idealmente, vorrei creare una sorta di tabella in cui ogni riga elenca la macro stessa, il risultato e un commento . Ecco un MWE che mostra cosa sto facendo attualmente e cosa mi piacerebbe fare:
\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}
Risposte
Non così diverso dalla proposta di Ulrich, ma con una codifica più semplice. La macro ha una variante * per il materiale da stampare in modalità matematica.
\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}
Il primo argomento viene assorbito alla lettera, ma poi viene scansionato di nuovo per essere utilizzato per la seconda colonna, possibilmente tra i $caratteri.
Ho modificato il mio esempio e, seguendo l'esempio di egreg, ho aggiunto un argomento asterisco:
Con \ExampleRow*{...il codice viene eseguito in modalità matematica.
Con \ExampleRow{...il codice viene eseguito in modalità matematica solo se il codice stesso contiene direttive per passare alla modalità matematica.
A differenza di egreg, tuttavia, non lo uso \tl_rescan:nn, ma lo uso ancora \scantokens, perché durante l'uso funzionano anche \scantokenscose come \verb|...|, che passano alla modalità verbatim. Con \tl_rescan:nnqueste cose non funzionano.
Le sottili ma cruciali differenze tra \scantokense \tl_rescan:nnsono trattate nelle domande
Qual è la differenza essenziale / cruciale tra \ scantokens / \ tex_scantokens: D e \ tl_rescan: nn?
e
Expl3 - come puoi passare il contenuto di una variabile-lista-token come argomento a un'altra "funzione"?
Poiché xparse è in uso, probabilmente puoi avere \ExampleRow
- leggere un argomento di tipo v (parola per parola),
- stampalo con
\verbatim@fontin vigore per mostrare la codifica - alimentalo a
\scantokens, annidato$e seguito da%(\scantokensinserisce\endlinechar...) per mostrare il risultato.
\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}
Forse qualcosa come il seguente? (questa volta niente spazi extra dopo i nomi delle macro)
\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}
Vecchia soluzione (i nomi delle macro saranno seguiti da spazi)
Forse qualcosa di simile
\newcommand\ExampleRow[2]{\texttt{\detokenize{#1}} & $#1$ & #2 \\}
è sufficiente per il tuo scopo?
\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}