9

Say \a and \b are two macros that expand to some text and either one may be \empty but not both. I want to put \a and \b in a line, centered and separated by a \quad.

\hfil\a\quad\b\hfil

Problem: if one of \a or \b are \empty, then the \quad prevents the text from being properly centered. Ideally I would need some glue between \a and \b that would shrink to zero if it came in direct contact with infinitely stretchable glue.

1
  • 1
    \hfil \a\quad \b\unskip \hfil can handle the "empty \b" case. – Ulrike Fischer Jul 3 '14 at 13:07
11

The definitions \a and \b can be tested, whether they are \empty:

\a
\ifx\a\empty
\else
  \ifx\b\empty
  \else
    \quad
  \fi
\fi
\b
5

You can remove the \quad before an empty \b with \unskip. You can test for an empty \a by checking the \gluestretchorder of \lastskip:

\documentclass[parskip=half-]{scrartcl}
\usepackage{lipsum}


\begin{document}
\lipsum[1]

\def\a{balbalbablabl} \def\b{balbalbablabl}

\hfil\a\ifnum\gluestretchorder\lastskip>0\else\quad\fi\b\unskip \hfil

\def\a{balbalbablabl} \def\b{}

\hfil\a\ifnum\gluestretchorder\lastskip>0\else\quad\fi\b\unskip \hfil

\def\a{} \def\b{balbalbablabl}

\hfil\a\ifnum\gluestretchorder\lastskip>0\else\quad\fi\b\unskip \hfil

\hfil balbalbablabl\hfil

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.