4

We can use \makebox[0pt]{text} to make a box that is logically 0pt width to external (as if nothing existed to the surroundings, while actually we can put whatever in it). My questions is---what is the counterpart command to make the height of a box appear to be 0pt, logically, to its adjacent boxes. With such a box, after a \newline, next line will appear at the vertical position of the previous line on the same page.

  • \raisebox{0pt}[0pt][0pt]{text}. – egreg Jan 11 at 8:55
  • @egreg Thanks. But I tried \raisebox{0pt}[0pt][0pt]{Some text}\par{More text} but didn't get what's expected. – Max Jan 11 at 9:16
  • 2
    Max you got what @egreg expected, but we don't know what you expected unless you say what you want or expect. – David Carlisle Jan 11 at 9:19
  • Haha, what I expected was the 'next' new line appearing at the vertical position of previous line (with 0pt height), i.e. overlapping. – Max Jan 11 at 9:21
  • @Max: You still two lines. The raisebox makes the content zero height but a text line is still generated with \baselineskip between it and the next line. If you want to overprint this way you need to add negative vertical space instead: like \par\vspace{-\baselineskip}. It is about if you are in horizontal mode (e.g. inside a text line) or vertical mode (e.g. between paragraphs). TeX does react differently in these modes. – Martin Scharrer Jan 11 at 10:11
7

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.

  • Wouldn't \vspace{\glueexpr-\baselineskip-\parskip} (or two negative \vspaces) still do the trick with \par and \flushbottom because the stretch/shrink components also cancel out? – Circumscribe Jan 12 at 18:45
  • @Circumscribe: Well, adding \parskip is a good idea, but might not enough, because AFAIK TeX might add more space in some cases. I would need to check The TeXBook again. – Martin Scharrer Jan 12 at 19:52
  • It is my understanding that in case of an underfull vbox the space that is added is still divided over the already present vertical skips in proportion to their stretch values and that this is no different for negative values. So whenever a \parskip is stretched, a manually inserted \vspace{-\parskip} on the same page would be anti-stretched (I'm deliberately not saying "shrunk") by the same amount. There could well be exceptions though. – Circumscribe Jan 12 at 20:39
5
\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

  • @Circumsrcibe, Yes, good idea, see edit. Initially I remained agnostic about what was the goal of the inserted box. – user4686 Jan 12 at 21:26
  • Makes sense, but I expect this'll probably match OP's expectations better. I'd +1 again if I could. – Circumscribe Jan 12 at 21:38
  • @Circumscribe I could circumscribe usage of \prevdepth. – user4686 Jan 12 at 22:08
  • Heh, having to know the value of \prevdepth when creating the box does seem undesirable. This last version no longer works if the line preceding the box is very deep or the one following it is very tall though. – Circumscribe Jan 12 at 22:25
  • @jfbu: Thanks. That's looks interesting. Can you please add some comments in the code? – Max Jan 13 at 9:09
3

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.

  • That's exactly the effect I meant and is interesting. But when I changed a bit `\makebox[1cm]{}\Huge{overlapping}\` it didn't work any more. – Max Jan 13 at 9:00
  • @Max: This is because \Huge also changes \baselineskip, \lineskip \lineskiplimit; it works if you use {\Huge overlapping} because these changes are then undone before the paragraph ends. You may also want to have a look at this question about the usage of \Huge and its ilk. – Circumscribe Jan 13 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.