在表格中,如何确定上一行已经结束?

在表格中,如何确定上一行已经结束?

如果一个人在一个tabular环境中,他如何能确定上一行已经结束(以\\)?

\hline人们可能只想在新行的开头添加或做一些其他的事情。

如果之前添加\\,则可能会添加不必要的额外空行。

所以问题是,与\clearpagevs.相比\newpage,是否有类似的tabular \???命令\\

答案1

下面引入了一个tabular new line 宏\tnl,它检查后面是否使用\tnl\@ifnextchar由于\@ifnextchar在输入流中留下了“偷看的标记”,因此\tnl除了最后一个之外,连接的 调用 都将被忽略:

在此处输入图片描述

\documentclass{article}

\makeatletter
\newcommand{\tnl}{\@ifnextchar\tnl{}{\\}}
\makeatother

\begin{document}

\begin{tabular}{ l }
  one \\ two
\end{tabular}\quad
\begin{tabular}{ l }
  one \tnl two
\end{tabular}

\bigskip

\begin{tabular}[t]{ l }
  one \\ \\ two
\end{tabular}\quad
\begin{tabular}[t]{ l }
  one \tnl \tnl two
\end{tabular}

\bigskip

\begin{tabular}[t]{ l }
  one \\ \\ \\ two
\end{tabular}\quad
\begin{tabular}[t]{ l }
  one \tnl \tnl \tnl two
\end{tabular}

\end{document}

这确实删除了随附的可选参数规范\\。不确定是否需要。

答案2

只是为了说明我提出的建议一条评论

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\newcommand*{\ensureLineHasEnded}{\ifvmode \else \expandafter \\\fi}
\newcommand*{\NLhline}{\ensureLineHasEnded\hline}



\begin{document}

\noindent
\begin{tabular}{|l|}
    \hline
    A one-column \texttt{tabular}, just for demo purposes.  \\
    \NLhline
    It uses both vertical and horizontal rules (arrggh\ldots!).  \\
    \NLhline
    But some of the \verb|\\|~commands are missing\ldots
    \NLhline
    \ldots yet the horizontal rules are typeset normally.  \\*[6pt]
    \NLhline
    The \verb|\ensureLineHasEnded| can take the same optional arguments\ldots
    \ensureLineHasEnded*[6pt]\hline
    \ldots as the \verb|\\|~command.
    \NLhline
    Moreover, it works as expected when used at the end of the \texttt{tabular}.
    \ensureLineHasEnded[9pt]
\end{tabular}\par

\end{document}

编辑:大卫·卡莱尔曾指出\ifvmode如果使用 -type 列,则测试不起作用p,因为 TeX 在这些列中也处于垂直模式。注意,更准确地说,TeX 是内部的垂直模式既包括p类型列内模式,也包括对齐线之间的模式(TeXbook,第 282 页),因此补充\ifinner测试也无济于事。在简单的情况下基于当前的组级别,可以通过 e-TeX 原语进行查询\currentgrouplevel

以下代码仅仅是一个概念证明:除非您知道它将被使用的精确环境,否则改进它就没有什么意义。

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\makeatletter

\@ifdefinable\@tabular@group@level{\newcount\@tabular@group@level}
\newcommand*\ensureLineHasEnded{%
    \ifnum \currentgrouplevel>\@tabular@group@level
        % % Diagnostics:
        % \count@ \currentgrouplevel
        % \typeout{Inserting \protect\\ at line \number\inputlineno,
        %     since \number\count@\space > \number\@tabular@group@level}%
        \expandafter \\%
    \fi
}
\newcommand*\saltAwayLevel{%
    \noalign{%
        \global \@tabular@group@level
            \numexpr \currentgrouplevel -\@ne \relax
        % % Diagnostics:
        % \typeout{\number\@tabular@group@level}
    }%
}

\makeatother

\newcommand*{\NLhline}{\ensureLineHasEnded\hline}



\begin{document}

With \texttt{l}~columns:
\begin{center}
    \begin{tabular}{|l|}
        \saltAwayLevel
        \hline
        A one-column \texttt{tabular}, just for demo purposes.  \\%
        \NLhline
        It uses both vertical and horizontal rules (arrggh\ldots!).  \\
        \NLhline
        But some of the \verb|\\|~commands are missing\ldots
        % \typeout{\number\currentgrouplevel}%
        \NLhline
        \ldots yet the horizontal rules are typeset normally.  \\*[6pt]
        \NLhline
        The \verb|\ensureLineHasEnded| can take the same optional arguments\ldots
        \ensureLineHasEnded*[6pt]\hline
        \ldots as the \verb|\\|~command.  \\
        \NLhline
        Moreover, it works as expected when used at the end of the \texttt{tabular}.
        \ensureLineHasEnded[9pt]
    \end{tabular}\par
\end{center}

\bigbreak

With \texttt{p}~columns:
\begin{center}
    \begin{tabular}{|p{.9\linewidth}|}
        \saltAwayLevel
        \hline
        A one-column \texttt{tabular}, just for demo purposes.  \\%
        \NLhline
        It uses both vertical and horizontal rules (arrggh\ldots!).  \\
        \NLhline
        But some of the \verb|\\|~commands are missing\ldots
        % \typeout{\number\currentgrouplevel}%
        \NLhline
        \ldots yet the horizontal rules are typeset normally.  \\*[6pt]
        \NLhline
        The \verb|\ensureLineHasEnded| can take the same optional arguments\ldots
        \ensureLineHasEnded*[6pt]\hline
        \ldots as the \verb|\\|~command.  \\
        \NLhline
        Moreover, it works as expected when used at the end of the \texttt{tabular}.
        \ensureLineHasEnded[9pt]
    \end{tabular}\par
\end{center}

\end{document}

您可以取消注释诊断命令以获取有关正在发生的事情的更多信息。

答案3

大多数情况下,您可以使用 TeX 原语\crcr来实现这一点。\crcr除非\cr前一个命令是,\cr否则它什么也不做。

\\这就是 Latex 可以忽略最后一行末尾的a 的原因的核心。

唯一的问题是,如果您以 结束上一行,\crcr那么它就好像以 结束一样,\cr而不是\\,但在大多数情况下,结果是相同的。

相关内容