如果一個人在一個tabular
環境中,如何確定前一行已經結束(以\\
)?
\hline
人們可能只想在新行的開頭添加或執行一些其他操作。
如果在之前添加\\
,可能會添加一個不需要的額外空白行。
所以問題是,與\clearpage
vs.相比, vs.\newpage
是否有類似的指令?tabular
\???
\\
答案1
下面介紹一個t
abular n
ew l
ine 宏\tnl
,它檢查後面是否\tnl
使用了\@ifnextchar
.由於\@ifnextchar
在輸入流中留下了“peeked token”,所以連接的呼叫\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
類型列內部和對齊線之間(教材,p。 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 的核心原因。
唯一的問題是,如果你用 then 結束前一行,就好像它是以而不是\crcr
結束的,但在大多數情況下,這會導致相同的事情。\cr
\\