![如何通过命令向表中添加行](https://linux22.com/image/462378/%E5%A6%82%E4%BD%95%E9%80%9A%E8%BF%87%E5%91%BD%E4%BB%A4%E5%90%91%E8%A1%A8%E4%B8%AD%E6%B7%BB%E5%8A%A0%E8%A1%8C.png)
我想在这里做一些与这个问题非常相似的事情: 使用宏向表中添加行
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
先展开宏。您可以使用选项执行此操作(请参阅包手册的第 3.2.3 节“首先展开宏” ):\@history
expand
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}