1

I want to insert a block of text (could contain anything) that takes up no vertical space, but also aligns with the normal text baseline. (I feel like this is probably a duplicate, but I can't it.)

\vbox to 0pt is as close as I can get, but it aligns with the top of the box rather than the text baseline. I find the extra space (height of tallest character in first line) this creates annoying and ugly.

The goal is to insert answers below questions without disturbing the layout obtained when no answers are shown. Any method of achieving this is acceptable.

MWE

What I have so far:

\documentclass[twocolumn]{article}
\usepackage{ifluatex}
\ifluatex
  \usepackage{lua-visual-debug}
\fi
\usepackage{enumitem}
\setlist[enumerate]{itemsep=2cm}
\newdimen\savedparindent
\newdimen\savedparskip
\newcommand{\blap}[1]{%
  \savedparindent\parindent
  \savedparskip\parskip
  \noindent
  \leavevmode\vbox to 0pt{%
    \hsize\linewidth\noindent\parbox{\linewidth}{%
      \parindent\savedparindent
      \parskip\savedparskip
      #1}%
    \vss}%
  \vspace*{-\parskip}\vspace*{-\baselineskip}}
\pagestyle{empty}
\begin{document}
\begin{enumerate}
  \item First item.

    \blap{This should not take up any vertical space, but should align at
    normal text baseline.}

  \item Second item.
\end{enumerate}

\blap{This should not take up any vertical space, but should align at normal
text baseline.}

\vspace{2cm}

More text.

\newpage

\begin{enumerate}
  \item First item.
  \item Second item.
\end{enumerate}

\vspace{2cm}

More text.

\end{document}
David Purton
  • 25,884
  • Does replacing \vbox to 0pt with \vtop to 0pt do what you want? My test on your example seems to work. – corporal Jun 29 '18 at 01:54

1 Answers1

4

I suggest working with one code format: Choose either TeX code or LaTeX code. Here is a solution using LaTeX:

\documentclass[twocolumn]{article}
\usepackage{ifluatex}
\ifluatex
  \usepackage{lua-visual-debug}
\fi
\usepackage{enumitem}
\setlist[enumerate]{itemsep=2cm}
\newdimen\savedparindent
\newdimen\savedparskip
\newcommand{\blap}[1]{%
  \savedparindent\parindent
  \savedparskip\parskip
  \noindent
  \parbox[t][0pt]{\linewidth}{%
      \parindent\savedparindent
      \parskip\savedparskip
      #1}%
  \vspace*{-\parskip}\vspace*{-\baselineskip}}
\pagestyle{empty}
\begin{document}
\begin{enumerate}[topsep=0pt,partopsep=\parsep]
  \item First item.

    \blap{This should not take up any vertical space, but should align at
    normal text baseline.}

  \item Second item.
\end{enumerate}

\blap{This should not take up any vertical space, but should align at normal
text baseline.}

\vspace{2cm}

More text.

\newpage

\begin{enumerate}
  \item First item.
  \item Second item.
\end{enumerate}

\vspace{2cm}

More text.

\end{document}

Output

enter image description here

Ruixi Zhang
  • 9,553
  • Yes, that is better I agree! Thank you. I don't think it will be a problem for me, but if the height of the first line increases beyond \baselineskip - \lineskip, then the \parbox will still take up a little space. Is there any way to account for this? e.g., can you easily calculate this height, and then add another \vspace at the end of the macro? – David Purton Jun 29 '18 at 02:33
  • Doing it without the negative \vspace commands would also help, as these still seem to mess with vertical spacing a bit in some situations in my actual document. – David Purton Jun 29 '18 at 02:58
  • 2
    @DavidPurton I’d probably \smash the ones that are too high, which are rare. So, if $\int^{b^{2^n}}$ unfortunately appears in the first line, I’d write \smash{$\int^{b^{2^n}}$} instead. A well-designed font and a well-designed class (with well-designed line spacing) usually are capable of avoiding increasing the line height. If they fail, then it’s probably due to large formulae, which should be displayed anyway. – Ruixi Zhang Jun 29 '18 at 03:00
  • That's true, regarding height of first line. – David Purton Jun 29 '18 at 03:06
  • Ah! if I add \aftergroup\ignorespaces, it deals with my remaining issues. I'll pay your answer! – David Purton Jun 29 '18 at 03:09
  • @DavidPurton You may check this answer, which showed the dimensions used when formatting lists. Be aware of \partopsep and \parsep. – Ruixi Zhang Jun 29 '18 at 03:13