宏(新命令)为什么有些文本没有出现在输出中?

宏(新命令)为什么有些文本没有出现在输出中?

我想在同一页上重复一些文本和表格。我正在尝试下面给出的代码,但它只打印一次,而且最后没有打印标尺。完整的代码如下。

\documentclass{article}
\usepackage{multirow}
\usepackage{caption}
\usepackage{subfig}
\usepackage{makecell}
\usepackage{booktabs}
\usepackage{tikz}
\usepackage{tabu}
\usepackage{array}
\usepackage{graphicx}
\usepackage[showframe]{geometry}
\usepackage{floatrow}
\floatsetup[table]{capposition=top}
\captionsetup{skip=0pt}

\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{xparse}% http://ctan.org/pkg/xparse

% Table float box with bottom caption, box width adjusted to content
\newfloatcommand{capbtabbox}{table}[][\FBwidth]

\usepackage{geometry}
 \geometry{
 a4paper,
 total={210mm,297mm},
 left=5mm,
 right=5mm,
 top=2cm,
 bottom=5mm,
 }

\usetikzlibrary{arrows}
\usetikzlibrary{shapes}

\NewDocumentCommand{\myrule}{O{1pt} O{3pt} O{black}}{%
  \par\nobreak % don't break a page here
  \kern\the\prevdepth % don't take into account the depth of the preceding line
  \kern#2 % space before the rule
  {\color{#3}\hrule height #1 width\hsize} % the rule
  \kern#2 % space after the rule
  \nointerlineskip % no additional space after the rule
}

\newcommand\textbox[1]{%
  \parbox{1\textwidth}{#1}%
}

\newcommand\insertBigText[1]{
 \noindent\textbox{\hfil \Large{\textbf{#1}}\hfil} \\

\myrule[1pt][2pt]
\vspace{5pt}
\vspace*{-\baselineskip}

\begin{table}[!htbp]
\parbox{0.45\linewidth}{
  \begin{tabu} spread 0pt {|*{2}{[2pt]X[c,m]|}}
     \hline
     \multicolumn{1}{l}{\textbf{text 1}} & \vspace{5mm}\\ \hline
     \multicolumn{1}{l}{\textbf{text 2}} &  \vspace{5mm} \\\hline 
     \multicolumn{1}{l}{\textbf{text 3 }} &  \vspace{5mm}\\\hline
     \multicolumn{1}{l}{\textbf{text 4}} & some more text\\\hline

    \end{tabu}}
\hfill
\parbox{0.45\linewidth}{
\centering
  \begin{tabu} spread 0pt {|*{1}{[2pt]X[c,m]|}}
     \textbf{string 1} \\\hline 
     \textbf{string 2} \\\hline 
      string 3 \\\hline
    \end{tabu}}
\end{table}

\myrule[1pt][2pt] % strange! doesn't get printed at all
} %end \newcommand\insertBigText[1]{

% ------------ document start
\begin{document}
\captionsetup[table]{skip=0pt}  
\insertBigText{receipt} % only this part gets printed
\vspace{2mm}
\noindent\textbox{\hfil \Large{\textbf{counterfoil}}\hfil} \\ % strange! doesn't get printed at all
\myrule[1pt][2pt] % strange! doesn't get printed at all
\insertBigText{counterfoil} % strange! doesn't get printed at all


\end{document}

答案1

这里的主要问题是

\kern\the\prevdepth % don't take into account the depth of the preceding line

在 的定义中\myrule。要理解这个问题,看看TeXbook(第 79 页底部的双重危险弯曲)说明了\prevdepth

但是,\prevdepth设置为−1000pt 垂直列表开头或规则框之后的标记值;这用于抑制下一个行间粘连。

因此,第二次使用命令时,您\prevdepth会在排版材料之前-1000pt垂直跳过-1000pt(大约-14in!);材料显然被向上推出了页面。

除此之外,代码中还存在其他问题,例如使用\\后跟一个空行以及使用\Large带有参数的命令。我修复了这些问题(但可能需要进行调整,具体取决于您的实际意图)。

以下是代码的简化版本

\documentclass{article}
\usepackage{tabu}
\usepackage[showframe]{geometry}

\usepackage{xparse}% http://ctan.org/pkg/xparse

\usepackage{geometry}
\geometry{
 a4paper,
 total={210mm,297mm},
 left=5mm,
 right=5mm,
 top=2cm,
 bottom=5mm,
 }

\NewDocumentCommand{\myrule}{O{1pt} O{3pt} O{black}}{%
  \par\nobreak % don't break a page here%
  \kern#2 % space before the rule
  {\color{#3}\hrule height #1 width\hsize} % the rule
  \kern#2 % space after the rule
  \nointerlineskip % no additional space after the rule
}

\newcommand\textbox[1]{%
  \parbox{1\textwidth}{#1}%
}

\newcommand\insertBigText[1]{
\noindent\textbox{\hfil{\Large\textbf{#1}}\hfil}\par
\myrule[1pt][2pt]
\vspace{5pt}
\vspace*{-\baselineskip}
\begin{table}[!htbp]
\parbox{0.45\linewidth}{%
  \begin{tabu} spread 0pt {|*{2}{[2pt]X[c,m]|}}
     \hline
     \multicolumn{1}{l}{\textbf{text 1}} & \vspace{5mm}\\ \hline
     \multicolumn{1}{l}{\textbf{text 2}} &  \vspace{5mm} \\\hline 
     \multicolumn{1}{l}{\textbf{text 3 }} &  \vspace{5mm}\\\hline
     \multicolumn{1}{l}{\textbf{text 4}} & some more text\\\hline
    \end{tabu}}
\hfill
\parbox{0.45\linewidth}{%
\centering
  \begin{tabu} spread 0pt {|*{1}{[2pt]X[c,m]|}}
     \textbf{string 1} \\\hline 
     \textbf{string 2} \\\hline 
      string 3 \\\hline
    \end{tabu}}
\end{table}
\myrule[1pt][2pt]%
}

\begin{document}

\insertBigText{receipt}
\vspace{2mm}
\myrule[1pt][2pt]
\insertBigText{counterfoil}

\end{document}

输出:

在此处输入图片描述

您仍会收到两个警告geometry

Package geometry Warning: Over-specification in `h'-direction. `width'
(597.50787pt) is ignored.

Package geometry Warning: Over-specification in `v'-direction. `height' 
(845.04684pt) is ignored.

因此您需要检查所使用的设置。

更新

作为埃格尔通知his comment您可以使用条件测试来安全地\kern在您的定义中拥有:

\NewDocumentCommand{\myrule}{O{1pt} O{3pt} O{black}}{%
  \par\nobreak % don't break a page here%
  \ifdim\prevdepth>-1000pt\kern\prevdepth\fi
  \kern#2 % space before the rule
  {\color{#3}\hrule height #1 width\hsize} % the rule
  \kern#2 % space after the rule
  \nointerlineskip % no additional space after the rule
}

相关内容