有人知道如何添加\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
之前的使用\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
: