2
\documentclass{article}
\usepackage{tcolorbox}
\usepackage{tabularray}
\raggedright
\newcommand\secondline{
  \begin{tcolorbox}
    Line 2
  \end{tcolorbox}
}
\begin{document}
\begin{tblr}{colspec=X}
  Line 1
\end{tblr}

\secondline
\end{document}

Overleaf generates this 1 when I give it the above code. However, if I remove the empty line, Overleaf generates this 2, increasing the spacing between the table and the box. Why does it do this, and are there other situations where this happens?

0

2 Answers 2

5

You have asked "why". I try to explain this.

The LaTeX macro tblr opens horizontal mode without indentation (does \noindent) and puts here a box with given contents with the width equal to \hsize. It doesn't close the opened horizontal mode. Then the space after \end{tblr} follows and then empty line, which runs \par. This TeX primitive command removes the last space (i.e. the space from the end of the line after \end{tblr}) and the remaining material fits to \hsize width and single-line paragraph is created. The space in your macro (after \secondline{ ) is processed in vertical mode and does nothing.

The second case (without empty line): you have two spaces after tblr box in horizontal mode. First one is from the end of \end{tblr} and second from our macro. Then tcolorbox macro runs \par before the box is created. So you have "space space \par". The \par primitive removes only single space, i.e. one space remains after tblr box. The paragraph is created with two lines because tblr box plus following space doesn't fit to \hsize.

From TeX primitive point of view, your first case can be emulated by

\noindent\hbox to\hsize{Line 1\hss}\space\par \space\par\hbox{Line2}

and your second case can be emulated by

\noindent\hbox to\hsize{Line 1\hss}\space \space\par\hbox{Line2}

The new LaTeX is more complicated, because \par isn't TeX primitive, but this doesn't mater for this issue.

5

Your issue here is an improper definition of \secondline that has a spurious space before the tcolorbox starts. In short, use this definition:

\newcommand\secondline{% <----- See https://tex.stackexchange.com/q/7453/5764
  \begin{tcolorbox}
    Line 2
  \end{tcolorbox}
}

If you don't include the %, a space is added before the tcolorbox is set. And since it is preceded by an X-wide tabular (which spans the entire text block width), any space inserted after it (when \secondline follows immediately after the tblr) will cause an additional line break and therefore "extra vertical space."

Avoid it by using proper definitions without spurious spaces.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .