How to make a 0pt height box?

To answer your written question: you can remove the height of a box by storing into a box register and set the height to 0pt and then use that box. You might also want to set the depth to 0pt. Ways of easily do this is the adjustbox package: \adjustbox{set height=0pt, set depth=0pt}{Some Text} or, without a package, \raisebox{0pt}[0pt][0pt]{Some Text}.

Note that this just makes the box have no official vertical size, but it is still placed in a horizontal list which makes up the line. Inside a paragraph all characters and other material like images are modeled by TeX as boxes with a height, depth and width and placed on a list which makes up a line, until enough is there to build a full line. All of these boxes sit on a line, the baseline, everything above that line makes up the height, everything below the depth. TeX usually places the amount \baselineskip between the baselines of two sequential lines.

Therefore if you want to overprint the following line over the last line, deleting the official height and depth of the content of the first line has no effect, because the baselineskip is still added. In order to achieve this you need to undo the baselineskip again so that the second line starts at the same vertical position as the first. This can be done using \vspace{-\baselineskip} between the two lines, in most cases. Note that if the first line contains material with a very large depth, then the two lines will be more than \baselineskip apart! Also if you have a paragraph break there then LaTeX might add more space between the last line of the last paragraph and the first line of the following paragraph depending on global settings (e.g. using the parskip package; used to get German paragraph style). Also LaTeX tries to spread the paragraphs across the page if \flushbottom is active by increasing inter-paragraph spacing. In such cases, you might want to use \\ instead of \par.


\documentclass{article}
\usepackage{color}
\newbox\mybox
\begin{document}
Some text pppp

Some text pppp

\setbox\mybox\vbox{% some arbitrary contents
   \begingroup\color{red}
   % some paragraphs
     \hspace*{5cm}aaa, bbb, ccc\par 
     \hspace*{5cm}ppp, qqq, rrr\par
   % some vertical space
     \vspace*{1cm}
   % some more paragraphs
     \hspace*{5cm}uuu, vvv, www\par 
     \hspace*{5cm}xxx, yyy, zzz\par
   \endgroup}
% set the box to occupy zero space
\ht\mybox0pt
\dp\mybox0pt
% insert it in a way keeping \prevdepth
\setbox\mybox\vbox{\box\mybox}
\unvbox\mybox

Some text pppp

Some text pppp
\end{document}

enter image description here

Circumscribe suggestion: (if inserted contents are also paragraph like, hence one may want some alignment)

\documentclass{article}
\usepackage{color}
\newbox\mybox
\newdimen\myprevdepth
\begin{document}
Some text pppp

Some text pppp

\myprevdepth\prevdepth
\setbox\mybox\vbox{% some arbitrary contents
   \prevdepth\myprevdepth
   \begingroup\color{red}
   % some paragraphs
     \hspace*{5cm}aaa, bbb, ccc\par 
     \hspace*{5cm}ppp, qqq, rrr\par
   % some vertical space
     \vspace{2\baselineskip}
   % some more paragraphs
     \hspace*{5cm}uuu, vvv, www\par
     \hspace*{5cm}xxx, yyy, zzz\dotfill\par
   \endgroup}
% set the box to occupy zero space
\ht\mybox0pt
\dp\mybox0pt
% insert it in a way keeping \prevdepth
\setbox\mybox\vbox{\box\mybox}
\unvbox\mybox

Some text pppp\dotfill

Some text pppp\dotfill

And more paragraphs...

\mbox{}\cleaders \hbox to .44em{\hss .\hss }\hskip5cm This is regular
text\dotfill\newline which continues here\dotfill

\end{document}

enter image description here

I am adding a variant which avoids the \unvbox trick and needs no handling of \prevdepth.

\documentclass{article}
\usepackage{color}
\newbox\mybox
\begin{document}\parskip2pt
Some text pppp (parskip is 2pt in this example)

Some text\rule{1cm}{1pt} pppp

\setbox\mybox\vtop{% some arbitrary contents
   \hrule height 0pt
   \vbox{}\vskip-\baselineskip
   \begingroup\color{red}
   % some paragraphs
     \hspace*{5cm}aaa, bbb, ccc\par 
     \hspace*{5cm}ppp, qqq, rrr\par
   % some vertical space
     \vspace{\dimexpr2\baselineskip+\parskip\relax}
   % some more paragraphs
     \hspace*{5cm}uuu, vvv, www\par
     \hspace*{5cm}xxx, yyy, zzz\dotfill\par
   \endgroup}
% set the box to occupy zero space
\ht\mybox0pt
\dp\mybox0pt
\box\mybox % this will get the correct baseline
\vskip-\baselineskip

Some text pppp\smash{\rule{1pt}{\dimexpr\baselineskip+\parskip}}\dotfill

Some text pppp\dotfill

And more paragraphs...

\mbox{}\cleaders \hbox to .44em{\hss .\hss }\hskip5cm This is regular
text\dotfill\newline which continues here\dotfill

\end{document}

enter image description here


This is essentially just a comment, but it doesn’t fit in 600 characters: is it this what you are trying to achieve?

% 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.

\newenvironment*{overlap}{%
    \par
    \null
    \setlength\baselineskip{0pt}%
    \setlength\lineskip{0pt}%
    \setlength\lineskiplimit{-\maxdimen}%
    \setlength\parskip{0pt}%
}{\par}



\begin{document}

This is too long for a comment: is it this what you expected?

\begin{overlap}
    \noindent A few\\
    \makebox[1cm]{}overlapping\\
    \makebox[3cm]{}lines.

    ..................................And another paragraph.
\end{overlap}
More text.

\end{document}

I forgot: this is the output.

Output of the code


The OP asked for a version of the above that doesn’t break down when the font size is changed; here it is:

% 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.

\usepackage{color}

\newenvironment*{overlap}{%
    \par
    \null
    \linespread{0}\selectfont
    \setlength\lineskip{0pt}%
    \setlength\lineskiplimit{-\maxdimen}%
    \setlength\parskip{0pt}%
}{\par}



\begin{document}

This is too long for a comment: is it this what you expected?
\begin{overlap}
    \noindent A few\\
    \makebox[1cm]{}overlapping\\
    \makebox[3cm]{}lines.

    ..................................And another paragraph.
\end{overlap}
More text.

Now it works also if you change the font size:
\begin{overlap}
    \noindent A few\\
    \Large \makebox[1cm]{}overlapping\\
    \makebox[3cm]{}lines.

    \color{cyan}\Huge ..................................And another paragraph.
\end{overlap}
Still more text.

\end{document}

The corrseponding output:

Output of the second code sample

I’m not (yet) going to provide explanations, because it is still unclear whether or not this is what the OP is trying to achieve.

Tags:

Height

Box