如何透過命令向表格添加行

如何透過命令向表格添加行

我想做一些與這個問題非常相似的事情: 使用巨集向表新增行

但是,我無法讓它與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}

在此輸入影像描述

相關內容