2

\vspace at the beginning of a real page is canceled but at the beginning of a minipage it is faithfully added.

How could I modify \vspace (or the minipage-environment) to behave in the same way as at a real page break, i.e. I want the vspace ignored first in the minipage but honored anywhere else.

\documentclass{book}
\usepackage{showframe}

\begin{document}

\clearpage \vspace{1cm} test

\clearpage \begin{minipage}{\textwidth} \vspace{1cm} %should be ignored test \vspace{1cm} %should be kept test \end{minipage}

\end{document}

Bernard
  • 271,350
Florian
  • 2,919

1 Answers1

2
\documentclass{book}
\usepackage{showframe}

\makeatletter
\newcommand\myvspace[1]{\if@minipage\else\vspace{#1}\fi}
\makeatother

\begin{document}

\clearpage
\vspace{1cm}
test

\clearpage
\noindent\begin{minipage}{\textwidth}
\myvspace{1cm} %should be ignored
test
\myvspace{1cm} %should be kept
test
\end{minipage}

\end{document}

I left it as you had it but did you intend a paragraph break so there was vertical space between the test ? You can not see the second vspace as it comes after the text.

The \if@minpage is true at the start of a minipage and set false at the first paragraph, it is used.

As a perhaps better alternative to defining your own command you could use the standard LaTeX \addvspace command which already includes this test (you would then have to have a paragraph break though as \addvspace in horizontal mode is an error.

\documentclass{book}
\usepackage{showframe}

\begin{document}

\clearpage \addvspace{1cm} test

\clearpage \noindent\begin{minipage}{\textwidth} \addvspace{1cm} %should be ignored test

\addvspace{1cm} %should be kept test \end{minipage}

\end{document}

enter image description here

David Carlisle
  • 757,742
  • Thanks a lot! Oh yes, I certainly wanted the space between the two "test" in the minipage; that was a stupid copy-paste mistake that I left out the empty line. \addvspace will do very well for me -- I just didn't know that it would do the check. Is there perhaps a comprehensive source somewhere about all the spacing commands with their specific properties/differences? – Florian Jan 28 '21 at 09:54
  • Would the \if@minipage work for all environments? – Florian Jan 28 '21 at 09:56
  • 1
    @Florian for a list perhaps this even though egreg wrote it:-) https://tex.stackexchange.com/questions/41476/lengths-and-when-to-use-them/41488#41488 I am not sure what you mean, \if@minipage is just set by minipage. – David Carlisle Jan 28 '21 at 10:01
  • Thanks again! The list is very helpful though obviously not covering everything: To know that \addvspace will be canceled out by minipages I'll still have to ask you ;) As for the other question I was wondering whether I can expect an equivavalent variable set by every environment (something like \if@quote) which I could use to do things. – Florian Jan 28 '21 at 10:11
  • 1
    @Florian no it is specifically set by minipage but checked by environments like quote and lists so that they behave at the top of a minipage like they behave at the top of a page – David Carlisle Jan 28 '21 at 10:13
  • 1
    @Florian in the very latest latex release every enviornment does have a hook at the start and end at which you can add code, but that is rather different. – David Carlisle Jan 28 '21 at 10:14