如何在\edef巨集中使用\hline產生表格

如何在\edef巨集中使用\hline產生表格

有人知道如何添加到\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}

作為替代方案,您可以擴展和消除\protectbefore 使用\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:

在此輸入影像描述

相關內容