有人知道如何添加到\hline
用於生成表的巨集嗎?
我的MWE
\documentclass{article}
\RequirePackage{tabularx}
\makeatletter
\newcounter{appdocs}
\setcounter{appdocs}{1}
\renewcommand{\theappdocs}{AD\arabic{appdocs}}
\newcommand{\appdocsCnt}[1]{%
\theappdocs% Print counter
\refstepcounter{appdocs}\label{#1}}% Mark with label
\newcommand{\applicabledocumententries}{}
\newcommand{\applicabledocument}[3]{%
\protected@xdef\applicabledocumententries{\applicabledocumententries \protect\appdocsCnt{#1} & #2 & #3 \protect\\}}
\newcommand{\applicabledocumentstable}{%
\begin{tabularx}{\textwidth}{p{2cm}|X|p{4cm}}%9.8
\hline
{\bfseries Ref.} & {\bfseries Title} & {\bfseries Reference and Issue} \\\hline
\applicabledocumententries
\hline
\end{tabularx}
}
\makeatother
\begin{document}
\applicabledocument{ad:1}{myTexta}{someDate}
\applicabledocument{ad:2}{myTextb}{anotherDate}
\applicabledocument{ad:3}{myTextc}{randomDate}
\applicabledocumentstable
\end{document}
產生
但我想\hline
在列表的每個條目之後都有。如何將 \hline 新增到巨集擴充功能?可能會進入正確的方向,但我無法將其應用於我的範例。
答案1
您可以使用\DeclareRobustCommand
以下命令來定義穩健的\hline
或(如範例所示)表格線端\hline
:
\documentclass{article}
\usepackage{tabularx}% Don't use \RequirePackage in document preamble after \documentclass
\makeatletter
\newcounter{appdocs}
\setcounter{appdocs}{1}
\renewcommand{\theappdocs}{AD\arabic{appdocs}}
\newcommand{\appdocsCnt}[1]{%
\theappdocs% Print counter
\refstepcounter{appdocs}\label{#1}}% Mark with label
\newcommand{\applicabledocumententries}{}
\DeclareRobustCommand*{\tabularnewlinewithhline}{\\\hline}
\newcommand{\applicabledocument}[3]{%
\protected@xdef\applicabledocumententries{\applicabledocumententries \protect\appdocsCnt{#1} & #2 & #3 \tabularnewlinewithhline}}
\newcommand{\applicabledocumentstable}{%
\begin{tabularx}{\textwidth}{p{2cm}|X|p{4cm}}%9.8
\hline
{\bfseries Ref.} & {\bfseries Title} & {\bfseries Reference and Issue} \\\hline
\applicabledocumententries
\end{tabularx}
}
\makeatother
\begin{document}
\applicabledocument{ad:1}{myTexta}{someDate}
\applicabledocument{ad:2}{myTextb}{anotherDate}
\applicabledocument{ad:3}{myTextc}{randomDate}
\applicabledocumentstable
\end{document}
作為替代方案,您可以擴展和消除\protect
before 使用\applicabledocumententries
:
\documentclass{article}
\usepackage{tabularx}
\makeatletter
\newcounter{appdocs}
\setcounter{appdocs}{1}
\renewcommand{\theappdocs}{AD\arabic{appdocs}}
\newcommand{\appdocsCnt}[1]{%
\theappdocs% Print counter
\refstepcounter{appdocs}\label{#1}}% Mark with label
\newcommand{\applicabledocumententries}{}
\DeclareRobustCommand*{\tabularnewlinewithhline}{\\\hline}
\newcommand{\applicabledocument}[3]{%
\protected@xdef\applicabledocumententries{\applicabledocumententries \protect\appdocsCnt{#1} & #2 & #3 \protect\\\protect\hline}}
\newcommand{\applicabledocumentstable}{%
\begin{tabularx}{\textwidth}{p{2cm}|X|p{4cm}}%9.8
\hline
{\bfseries Ref.} & {\bfseries Title} & {\bfseries Reference and Issue}
\\\hline
\let\protect\noexpand
\edef\applicabledocumententries{\applicabledocumententries}%
\applicabledocumententries
\end{tabularx}
}
\makeatother
\begin{document}
\applicabledocument{ad:1}{myTexta}{someDate}
\applicabledocument{ad:2}{myTextb}{anotherDate}
\applicabledocument{ad:3}{myTextc}{randomDate}
\applicabledocumentstable
\end{document}
答案2
下面我插入了一個宏\trules
,該宏最初設定為\relax
(不可擴展)作為\applicabledocumententries
構造的一部分。\trules
然後設定\hline
為\applicabledocumentstable
:
\documentclass{article}
\usepackage{tabularx}
\makeatletter
\newcounter{appdocs}
\renewcommand{\theappdocs}{AD\arabic{appdocs}}
\newcommand{\appdocsCnt}[1]{%
\mbox{}\refstepcounter{appdocs}\label{#1}% Mark with label
\theappdocs% Print counter
}
\newcommand{\applicabledocumententries}{}
\let\trules\relax
\newcommand{\applicabledocument}[3]{%
\protected@xdef\applicabledocumententries{%
\applicabledocumententries \protect\appdocsCnt{#1} & #2 & #3 \protect\\ \trules}}
\makeatother
\newcommand{\applicabledocumentstable}{%
\let\trules\hline
\begin{tabularx}{\textwidth}{ p{2cm} | X | p{4cm} }
\hline
{\bfseries Ref.} & {\bfseries Title} & {\bfseries Reference and Issue} \\
\hline
\applicabledocumententries
\end{tabularx}
}
\makeatother
\begin{document}
\applicabledocument{ad:1}{myTexta}{someDate}
\applicabledocument{ad:2}{myTextb}{anotherDate}
\applicabledocument{ad:3}{myTextc}{randomDate}
\noindent
\applicabledocumentstable
\end{document}
不過,如果沒有它,桌子看起來會好得多。這是一個使用的版本booktabs
: