Afficher les macros mathématiques et générer un tableau
J'écris de la documentation sur certaines macros que j'ai créées. (Je sais qu'il y aura probablement des commentaires sur les macros aussi, mais ce n'est pas ce que je demande.) Idéalement, j'aimerais créer une sorte de tableau où chaque ligne répertorie la macro elle-même, le résultat et un commentaire . Voici un MWE qui montre ce que je fais actuellement et ce que j'aimerais faire:
\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}

Réponses
Pas si différent de la proposition d'Ulrich, mais avec un codage plus simple. La macro a une * -variante pour le matériau à imprimer en mode mathématique.
\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}

Le premier argument est absorbé textuellement, mais il est ensuite réanalysé afin d'être utilisé pour la deuxième colonne, éventuellement entre les $
caractères.
J'ai édité mon exemple et - suivant l'exemple d'egreg - ajouté un argument astérisque:
Avec \ExampleRow*{...
le code est exécuté en mode mathématique.
Avec \ExampleRow{...
le code n'est exécuté en mode mathématique que si le code lui-même contient des directives pour passer en mode mathématique.
Contrairement à egreg, cependant, je n'utilise pas \tl_rescan:nn
, mais je l'utilise toujours \scantokens
, car lors de l'utilisation \scantokens
, des choses comme \verb|...|
, qui passent en mode verbatim, fonctionnent également. Avec de \tl_rescan:nn
telles choses ne fonctionnent pas.
Les différences subtiles mais cruciales entre \scantokens
et \tl_rescan:nn
sont traitées dans les questions
Quelle est la différence essentielle / cruciale entre \ scantokens / \ tex_scantokens: D et \ tl_rescan: nn?
et
expl3 - comment pouvez-vous passer le contenu d'une variable de liste de jetons comme argument à une autre "fonction"?
Comme xparse est utilisé, vous pouvez probablement avoir \ExampleRow
- lire un argument de type v (verbatim),
- l'imprimer avec
\verbatim@font
en effet pour montrer le codage - alimentez-le
\scantokens
, imbriqué$
et suivi par%
(\scantokens
insère-t-il\endlinechar
...) pour afficher le résultat.
\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}

Peut-être quelque chose comme ce qui suit? (cette fois pas d'espaces supplémentaires après les noms de 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}
Ancienne solution (les noms de macro seront suivis d'espaces)
Peut-être quelque chose comme
\newcommand\ExampleRow[2]{\texttt{\detokenize{#1}} & $#1$ & #2 \\}
est suffisant pour votre objectif?
\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}
