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.
- 305
3 Answers
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.
- 262,582
-
Wouldn't
\vspace{\glueexpr-\baselineskip-\parskip}(or two negative\vspaces) still do the trick with\parand\flushbottombecause the stretch/shrink components also cancel out? – Circumscribe Jan 12 '19 at 18:45 -
@Circumscribe: Well, adding
\parskipis 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 '19 at 19:52 -
It is my understanding that in case of an
underfull vboxthe 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\parskipis 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 '19 at 20:39
\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}
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}
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}
-
@Circumsrcibe, Yes, good idea, see edit. Initially I remained agnostic about what was the goal of the inserted box. – Jan 12 '19 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 '19 at 21:38
-
-
Heh, having to know the value of
\prevdepthwhen 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 '19 at 22:25 -
@jfbu: Thanks. That's looks interesting. Can you please add some comments in the code? – Max Jan 13 '19 at 09:09
-
@Circumscribe I don't understand your remark about line preceding the box (try with
Some text\rule[-50pt]{1cm}{1pt} ppppin my example) but of course I agree about the line following, because the box can have no idea where that line will be and makes a bet it will be positioned at next baseline (taking into account parskip). – Jan 13 '19 at 09:27 -
@Max there are comments already, where do you feel some are missing? could you please add a precise example of what you want to achieve, because I am a bit in the dark and 2/3 of my answer are devoted to aligning things which may or may not be relevant to your usecase. A crucial question is whether you want to use the box mid-paragraph (via some
\vadjustfor example) and for this we need some code to start with and expected result. – Jan 13 '19 at 09:30 -
@jfbu: With
Some text\rule[-50pt]{1cm}{1pt} ppppas the second line of text, the text below the box is positioned differently if\box\myboxand\vskip-\baselineskipare commented out. – Circumscribe Jan 13 '19 at 09:38 -
@Circumscribe ah I see what you mean you are right of course. It is inherent to the third method and (afaict) only re-injecting some
\prevdepthcontrol could work us out of that problem; then second method is better. But I don't know what is best method because I don't know what is exact goal! – Jan 13 '19 at 12:33
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.
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:
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.
- 21,558
-
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 '19 at 09:00 -
@Max: This is because
\Hugealso 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\Hugeand its ilk. – Circumscribe Jan 13 '19 at 15:05





\raisebox{0pt}[0pt][0pt]{text}. – egreg Jan 11 '19 at 08:55\raisebox{0pt}[0pt][0pt]{Some text}\par{More text}but didn't get what's expected. – Max Jan 11 '19 at 09:16\baselineskipbetween 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 '19 at 10:11\raiseboxor\makeboxyou can also use\adjustbox{<keys>}{<content>}from theadjustboxpackage, e.g.\adjustbox{set height=0pt}{Some Text}or\adjustbox{rlap}{Some Text}(laps to the right). – Martin Scharrer Jan 11 '19 at 10:20\vspace{-\baselineskip}worked exactly the way I meant. Thanks. – Max Jan 11 '19 at 21:32