4

I try using a specific section only if a variable is defined. Everything seems to work, however there is an additional space between the name of person B and the address, although the same code is used as for person A...

\documentclass{article}

\setlength{\parindent}{0pt}

\newcommand{\AnredeMieterA}{Mrs.}
\newcommand{\NameMieterA}{Person A}
\newcommand{\AdresseMieterA}{address A}
\newcommand{\AnredeMieterB}{Mrs.}
\newcommand{\NameMieterB}{Person B}
\newcommand{\AdresseMieterB}{address B}

\begin{document}

\medskip \textbf{\AnredeMieterA\ \NameMieterA}\\ 
\medskip wohnhaft \textbf{\AdresseMieterA}\\ 
\ifx\NameMieterB\undefined
\else
\medskip sowie \\
\medskip \textbf{\AnredeMieterB\ \NameMieterB}\\ 
\medskip wohnhaft \textbf{\AdresseMieterB}\\ 
\fi

\end{document}

Why?

enter image description here

8
  • You are adding \medskip space. \medskip Mrs.PersonB\\ adds space after the linebreak Commented Jun 4, 2023 at 23:16
  • 1
    the code for A is different as in \medskip Mrs.PersonA\\ the \medskipis in vertical mode so adds a vertical space at that point Commented Jun 4, 2023 at 23:20
  • @DavidCarlisle -- but there's a \medskip also between Mrs, Person A and the address, but no extra space there. ??? Commented Jun 4, 2023 at 23:24
  • 1
    try \medskip aaa bbb ccc ddd eee \medskip aaa bbb ccc ddd eee \medskip aaa bbb ccc ddd eee \medskip aaa bbb ccc ddd eee \medskip aaa bbb ccc ddd eee (only) the first \medskp adds space directly, if you use \vspace mid-paragraph it adds space after the current line. Commented Jun 4, 2023 at 23:33
  • 2
    use \par not \\ Commented Jun 4, 2023 at 23:35

1 Answer 1

7

\medskip is \vspace{\medskipamount} and usage of \vspace in horizontal mode does not add the vertical space immediately but after the current line.

Because \\ in justified text does not make a paragraph, you are still in horizontal mode after the \\. So the \medskip before wohnhaft adds the space before sowie not before wohnhaft.

As David explained in a comment, the result would be different with \par instead of \\. This is because, after \par you are always in vertical mode and in vertical mode \vspace adds the vertical space immediately:

\documentclass{article}

\setlength{\parindent}{0pt}

\newcommand{\AnredeMieterA}{Mrs.}
\newcommand{\NameMieterA}{Person A}
\newcommand{\AdresseMieterA}{address A}
\newcommand{\AnredeMieterB}{Mrs.}
\newcommand{\NameMieterB}{Person B}
\newcommand{\AdresseMieterB}{address B}

\begin{document}

\medskip \textbf{\AnredeMieterA\ \NameMieterA}\par
\medskip wohnhaft \textbf{\AdresseMieterA}\par
\ifx\NameMieterB\undefined
\else
\medskip sowie \par
\medskip \textbf{\AnredeMieterB\ \NameMieterB}\par
\medskip wohnhaft \textbf{\AdresseMieterB}\par
\fi

\end{document}

Another suggestion would be to use the optional argument of \\:

\documentclass{article}

\setlength{\parindent}{0pt}

\newcommand{\AnredeMieterA}{Mrs.}
\newcommand{\NameMieterA}{Person A}
\newcommand{\AdresseMieterA}{address A}
\newcommand{\AnredeMieterB}{Mrs.}
\newcommand{\NameMieterB}{Person B}
\newcommand{\AdresseMieterB}{address B}

\begin{document}

\textbf{\AnredeMieterA\ \NameMieterA}\\[\medskipamount] 
wohnhaft \textbf{\AdresseMieterA}\\[\medskipamount]
\ifx\NameMieterB\undefined
\else
sowie \\[\medskipamount]
\textbf{\AnredeMieterB\ \NameMieterB}\\[\medskipamount]
wohnhaft \textbf{\AdresseMieterB}
\fi

\end{document}

or

\documentclass{article}

\setlength{\parindent}{0pt}

\newcommand{\AnredeMieterA}{Mrs.}
\newcommand{\NameMieterA}{Person A}
\newcommand{\AdresseMieterA}{address A}
\newcommand{\AnredeMieterB}{Mrs.}
\newcommand{\NameMieterB}{Person B}
\newcommand{\AdresseMieterB}{address B}

\begin{document}

\textbf{\AnredeMieterA\ \NameMieterA}%
\\[\medskipamount] wohnhaft \textbf{\AdresseMieterA}%
\ifx\NameMieterB\undefined
\else
\\[\medskipamount] sowie
\\[\medskipamount] \textbf{\AnredeMieterB\ \NameMieterB}
\\[\medskipamount] wohnhaft \textbf{\AdresseMieterB}
\fi

\end{document}

But because, I would avoid \\ in justified text, I would suggest to explicitly switch to ragged text, either using \raggedright or:

\documentclass{article}

\newcommand{\AnredeMieterA}{Mrs.}
\newcommand{\NameMieterA}{Person A}
\newcommand{\AdresseMieterA}{address A}
\newcommand{\AnredeMieterB}{Mrs.}
\newcommand{\NameMieterB}{Person B}
\newcommand{\AdresseMieterB}{address B}

\begin{document}

\begin{flushleft}
\textbf{\AnredeMieterA\ \NameMieterA}%
\\[\medskipamount] wohnhaft \textbf{\AdresseMieterA}%
\ifx\NameMieterB\undefined
\else
\\[\medskipamount] sowie
\\[\medskipamount] \textbf{\AnredeMieterB\ \NameMieterB}
\\[\medskipamount] wohnhaft \textbf{\AdresseMieterB}
\fi
\end{flushleft}

\end{document}

enter image description here

All examples result in the same vertical space between the lines of the block, but may differ in vertical distance before or after the block, if there would be text before or after it. The first example could also differ, if \parskip would be changed.

1
  • Thanks for the extensive answer. That helped me understand everything better. In my case I am in a scrreprt document and in a \begin{addmargin} environment. There \par does not work. But with your help I was able to make it work! Commented Jun 5, 2023 at 10:37

You must log in to answer this question.

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