Zeigen Sie mathematische Makros an und führen Sie eine Tabelle aus

Dec 11 2020

Ich schreibe eine Dokumentation über einige Makros, die ich erstellt habe. (Ich weiß, dass es wahrscheinlich auch Kommentare zu den Makros geben wird, aber darum frage ich nicht.) Idealerweise möchte ich eine Art Tabelle erstellen, in der jede Zeile das Makro selbst, das Ergebnis und einen Kommentar auflistet . Hier ist eine MWE, die zeigt, was ich gerade mache und was ich gerne machen würde:

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

Antworten

3 egreg Dec 14 2020 at 07:20

Nicht so verschieden von Ulrichs Vorschlag, aber mit einer einfacheren Codierung. Das Makro hat eine * -Variante für Material, das im mathematischen Modus gedruckt werden soll.

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

Das erste Argument wird wörtlich aufgenommen, dann aber erneut gescannt, um für die zweite Spalte verwendet zu werden, möglicherweise zwischen $Zeichen.

3 UlrichDiez Dec 14 2020 at 04:41

Ich habe mein Beispiel bearbeitet und folgende egreg des Beispiel schöpf ein Sternchen Argument:
Mit \ExampleRow*{...dem Code im Mathematik - Modus ausgeführt wird.
Mit \ExampleRow{...dem Code nur in Mathematik - Modus ausgeführt wird , wenn der Code selbst enthält Richtlinien für zu Mathematik - Modus umgeschaltet wird .

Im Gegensatz zu Egreg verwende ich jedoch nicht \tl_rescan:nn, aber immer noch \scantokens, da bei der Verwendung \scantokensauch Dinge wie \verb|...|, die in den wörtlichen Modus wechseln, ebenfalls funktionieren. Mit \tl_rescan:nnsolchen Dingen funktioniert das nicht.

Die subtilen, aber entscheidenden Unterschiede zwischen \scantokensund \tl_rescan:nnwerden in den Fragen behandelt.
Was ist der wesentliche / entscheidende Unterschied zwischen \ scantokens / \ tex_scantokens: D und \ tl_rescan: nn?
und
expl3 - wie können Sie den Inhalt einer Token-Listenvariablen als Argument an eine andere "Funktion" übergeben?


Da xparse verwendet wird, können Sie wahrscheinlich haben \ExampleRow

  • Lesen Sie ein Argument vom Typ v (wörtlich).
  • Drucken Sie es mit \verbatim@font, um die Codierung anzuzeigen
  • Füttere es \scantokens, verschachtelt $und verfolgt von %( \scantokensfügt ein \endlinechar...), um das Ergebnis anzuzeigen.

 

\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

Vielleicht so etwas wie das Folgende? (diesmal keine zusätzlichen Leerzeichen nach Makronamen)

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

Alte Lösung (Makronamen werden durch Leerzeichen gefolgt)

Vielleicht so etwas wie

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

ist ausreichend für Ihren Zweck?

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