Mostra macros matemáticas e resulta em uma tabela

Dec 11 2020

Estou escrevendo alguma documentação sobre algumas macros que criei. (Eu sei que provavelmente haverá comentários sobre as macros também, mas não é sobre isso que estou perguntando.) Idealmente, eu gostaria de fazer uma espécie de tabela onde cada linha liste a própria macro, o resultado e um comentário . Aqui está um MWE que mostra o que estou fazendo atualmente e o que gostaria de fazer:

\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}

Respostas

3 egreg Dec 14 2020 at 07:20

Não muito diferente da proposta de Ulrich, mas com uma codificação mais simples. A macro tem uma variante * para o material a ser impresso no 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}

O primeiro argumento é absorvido literalmente, mas, em seguida, é verificado novamente para ser usado na segunda coluna, possivelmente entre os $caracteres.

3 UlrichDiez Dec 14 2020 at 04:41

Editei meu exemplo e - seguindo o exemplo da egreg - adicionei um argumento de asterisco:
Com \ExampleRow*{...o código é executado em modo matemático.
Com \ExampleRow{...o código, só é executado no modo matemático se o próprio código contiver diretivas para alternar para o modo matemático.

Ao contrário do egreg, no entanto, eu não uso \tl_rescan:nn, mas ainda uso \scantokens, porque ao usar \scantokenscoisas como \verb|...|, que mudam para o modo literal, também funcionam. Com \tl_rescan:nnessas coisas não funcionam.

As diferenças sutis mas cruciais entre \scantokense \tl_rescan:nnsão tratadas nas questões
Qual é a diferença essencial / crucial entre \ scantokens / \ tex_scantokens: D e \ tl_rescan: nn?
e
expl3 - como você pode passar o conteúdo de uma variável de lista de tokens como argumento para outra "função"?


Como xparse está em uso, você provavelmente pode ter \ExampleRow

  • leia um argumento do tipo v (literalmente),
  • imprima-o com \verbatim@fontefeito para mostrar a codificação
  • alimentá-lo para \scantokens, aninhado em $e seguido por %( \scantokensinsere \endlinechar...) para mostrar o 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}

2 gernot Dec 11 2020 at 16:27

Talvez algo como o seguinte? (desta vez, sem espaços extras após os nomes das 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}

Solução antiga (nomes de macro serão seguidos por espaços)

Talvez algo como

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

é suficiente para o seu 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}