ここで、この質問と非常によく似たことをしたいと思います。 マクロを使用してテーブルに行を追加する
tabularray
ただし、パッケージとxparse
コマンドを使用して作成されたバージョン ログ テーブルの場合は、これを機能させることができません。
私の現在の MNWE (最小限の動作しない例...) は次のとおりです。
\documentclass{article}
\usepackage{xparse}
\usepackage{tabularray}
\makeatletter
\let\@history\@empty
\NewDocumentCommand \historyentry {m m m} {%
\g@addto@macro\@history{#1 & #2 & #2\\}
}
\NewDocumentCommand \history {} {%
\begin{tblr}{colspec={l l l}}
Version & Date & Comment \\
\@history{}
\end{tblr}
}
\makeatother
\historyentry{1.0}{2023-03-01}{First release}
\historyentry{2.0}{2023-03-15}{Second release}
\begin{document}
\history
\end{document}
これを LuaLaTeX (または PDFLaTeX) を使用してコンパイルすると、次のエラーが発生します。
This is LuaHBTeX, Version 1.15.0 (TeX Live 2022)
restricted system commands enabled.
(./test.tex
LaTeX2e <2022-11-01> patch level 1
L3 programming layer <2023-01-24> (/home/mnoethe/.local/texlive/2022/texmf-dist/tex/latex/base/article.cls
Document Class: article 2022/07/02 v1.4n Standard LaTeX document class
(/home/mnoethe/.local/texlive/2022/texmf-dist/tex/latex/base/size10.clo)) (/home/mnoethe/.local/texlive/2022/texmf-dist/tex/latex/l3packages/xparse/xparse.sty (/home/mnoethe/.local/texlive/2022/texmf-dist/tex/latex/l3kernel/expl3.sty (/home/mnoethe/.local/texlive/2022/texmf-dist/tex/latex/l3backend/l3backend-luatex.def))) (/home/mnoethe/.local/texlive/2022/texmf-dist/tex/latex/tabularray/tabularray.sty) (./test.aux) (/home/mnoethe/.local/texlive/2022/texmf-dist/tex/latex/base/ts1cmr.fd)
! Misplaced alignment tab character &.
\@history ->1.0 &
2023-03-01 & 2023-03-01\\2.0 & 2023-03-15 & 2023-03-15\\
l.25 \history
?
これに関して何かご助力いただければ幸いです。
\begin{tabular}{l l l}
の代わりに標準を使用する場合、同じコードが機能します\begin{tblr}{colspec={l l l}}
。
答え1
tabularray
表形式をタイプセットする前に、まずマクロを展開するように指示する必要があります\@history
。これは、オプションを使用して実行できますexpand
(パッケージ マニュアルのセクション 3.2.3「マクロを最初に展開する」を参照tabularray
)。
\documentclass{article}
\usepackage{tabularray}
\makeatletter
\let\@history\@empty
\NewDocumentCommand \historyentry {m m m} {%
\g@addto@macro\@history{#1 & #2 & #3 \\}
}
\NewDocumentCommand \history {} {%
\begin{tblr}[expand=\@history]{colspec={l l l}}
Version & Date & Comment \\
\@history{}
\end{tblr}
}
\makeatother
\historyentry{1.0}{2023-03-01}{First release}
\historyentry{2.0}{2023-03-15}{Second release}
\begin{document}
\history
\end{document}