数学マクロを表示し、結果を表にします

Dec 11 2020

私が作成したいくつかのマクロに関するいくつかのドキュメントを書いています。(マクロについてもコメントがあると思いますが、それは私が求めていることではありません。)理想的には、各行にマクロ自体、結果、コメントがリストされた一種のテーブルを作成したいと思います。 。これが私が現在していることと私がしたいことを示すMWEです:

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

回答

3 egreg Dec 14 2020 at 07:20

Ulrichの提案とそれほど違いはありませんが、コーディングが単純です。マクロには、数学モードで印刷される資料の*バリアントがあります。

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

最初の引数は逐語的に吸収されますが、2番目の列、場合によっては$文字間で使用するために再スキャンされます。

3 UlrichDiez Dec 14 2020 at 04:41

:私は私の例と、次egregの例付加アスタリスク引数編集した
\ExampleRow*{...コードが数式モードで実行されます。
では\ExampleRow{...、コード自体は数式モードに切り替えるためのディレクティブが含まれている場合は、コードのみ数式モードで実行されます。

ただし、egregとは異なり、私はを使用しませんが、を\tl_rescan:nn使用します\scantokens。これは、を使用すると\scantokens\verb|...|逐語的モードに切り替わるなども機能するためです。その\tl_rescan:nnようなものでは動作しません。

間の微妙なしかし決定的な違い\scantokensとは、\tl_rescan:nn質問に扱われる
Dと\ tl_rescan:NN scantokens / \ tex_scantokens \との本質的な/決定的な違いは何ですか?
および
expl3-token-list-variableの内容を引数として別の「関数」に渡すにはどうすればよいですか?


xparseが使用されているので、おそらく \ExampleRow

  • タイプv(逐語的)の引数を読み、
  • \verbatim@fontコーディングを示すために有効に印刷します
  • 結果を表示するために\scantokens、にフィードし、ネストし$、末尾に%\scantokens挿入\endlinechar...)します。

 

\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

たぶん次のようなものですか?(今回はマクロ名の後に余分なスペースはありません)

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

古い解決策(マクロ名の後にスペースが続きます)

多分何かのような

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

あなたの目的には十分ですか?

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