# macro (newcommand) Why some text is not getting in the output ?

I want to repeat some text and tables on the same page. I am trying the code given below, but it is printing only once and then again it is not printing the ruler at the end. The complete code is given below.

\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}{
\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
\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}

• The problem is the line \kern\the\prevdepth % don't take into account the depth of the preceding line in your definition. Besides this, there are some other bad practices such as using  \\  followed by blank lines. – Gonzalo Medina Feb 21 '15 at 16:13
• To follow up Gonzalo's comment, in half the cases here \prevdepth is -1000pt which is a big negative skip to insert. Also \Large like all size commands does not take a {} argument. – David Carlisle Feb 21 '15 at 16:18
• @GonzaloMedina Thanks for pointing this out. I removed that line and it worked fine. Can you explain why this is so? Also turn your comment into answer. I will accept it. – jitm234 Feb 21 '15 at 16:25
• prevdepth=-1000pt is a special built in flag in tex-the-program for start of a vertical list. – David Carlisle Feb 21 '15 at 16:27
• @jitm234 I am writing an answer. – Gonzalo Medina Feb 21 '15 at 16:36

The main problem here is with the line

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


in the definition of \myrule. To understand the problem, look at what The TeXbook (double dangerous-bend at the bottom of page 79) says about \prevdepth:

However, \prevdepth is set to the sentinel value −1000pt at the beginning of a vertical list, or just after a rule box; this serves to suppress the next interline glue.

So the second time you use your command, \prevdepth is -1000pt and you do a vertical skip of -1000pt (approximately -14in!) before typesetting the material; the material, obviously is pushed upwards, out of the page.

Besides this, there are other problems with the code, such as using \\ followed by a blank line and using \Large as a command with arguments. I fixed them (but perhaps adjustments will be needed, depending on your actual intent).

Here's a reduced version of the code

\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}{%
\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
\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}


The output:

You'll still get two warnings from 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.


so you will need to check the settings used.

## Update

As egreg notices in his comment you can use a conditional test to safely have the \kern in your definition:

\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
}

• One could do \ifdim\prevdepth>-1000pt\kern\prevdepth\fi – egreg Feb 21 '15 at 16:57
• @egreg I added your suggestion to my answer. Thanks. – Gonzalo Medina Feb 21 '15 at 17:02