Typesetting a comma unless before a line break

I'm trying to create a template (.sty file) that will typeset

[value of macro 1], [value of macro 2]


if this expression does not exceed the length of the line, and

[value of macro 1]
[value of macro 2]


otherwise in a centered environment. My first idea was using \mbox to prevent linebreaks within the fields themselves, but that leaves me with the problem of figuring out how to make LaTeX ignore the comma if the line break occurs. I then tried to solve the problem using

\newcommand{\@macroa}{\mbox{[value of macro1]}}
\newcommand{\@macrob}{\mbox{[value of macro2]}}
\newcommand{\@macroc}{\mbox{[value of macro1], [value of macro2]}}
\discretionary{\@macroa}{\@macrob}{\@macroc}


which kind of does what I want, but with a major caveat - if a linebreak occurs, the text isn't centered anymore, but now gets typeset as

|                       [value of macro1]|
|[value of macro 2]                      |


and I can't figure out how to insert the proper glue without causing LaTeX to raise an Improper discretionary list. error...

Ouch, I have figured out my mistake - I wasn't in a centered environment after all, but using \hfils. Changing to \centering solved the problem for me. Guess I should revisit the source code once more next time before writing up a question... however, I think I'm just gonna leave this here, since Google didn't come up with useful results for this use case, so maybe it'll be useful to someone else one day. My apologies!

• It works for me if I replace \@macro1 (which is invalid) by \@macroa, and the same for the other two. – Phelype Oleinik Sep 3 '19 at 13:05
• @PhelypeOleinik I'm sorry, I inserted these numbers into the example by mistake. My .sty file uses macro names without numbers... my problem is with the alignment, as stated in the question. – Sora. Sep 3 '19 at 14:07

2 Answers

You can typeset the comma as if it were glue (using \xleaders); since glue disappears at line breaks, also the comma will.

\documentclass{article}

\newcommand{\disappearingcomma}{%
\leavevmode
\sbox0{,}%
\xleaders\copy0\hskip\wd0\relax
\space
}

\newcommand{\macroa}{Value of macro A}
\newcommand{\macrob}{Value of macro B}
\newcommand{\macroab}{\mbox{\macroa}\disappearingcomma\mbox{\macrob}}

\begin{document}

\begin{minipage}{6cm}
\centering
\macroab
\end{minipage}\vrule
\begin{minipage}{4cm}
\centering
\macroab
\end{minipage}

\end{document}


As @PhelypeOleinik has already pointed out in a comment, the macro names \@macro1, \@macro2, and \@macro3 are invalid -- the digits 0 thru 9 are not allowed in multiple-character TeX and LaTeX macro names. I therefore suggest you use \@macroa, \@macrob, and \@macroc instead.

\documentclass{article}
\usepackage[utf8]{inputenc} %
\makeatletter
\newcommand{\@macroa}{\mbox{[value of macro A]}}
\newcommand{\@macrob}{\mbox{[value of macro B]}}
\newcommand{\@macroc}{\mbox{[value of macro A], [value of macro B]}}
\newcommand\blurb{\discretionary{\@macroa}{\@macrob}{\@macroc}}
%% Or, far more succinctly, David Carlisle's excellent suggestion:
%%   \newcommand\blurb{\@macroa\discretionary{}{}{\hbox{, }}\@macrob}
%% With this approach, it's not necessary to define '\@macroc'.
\makeatother

\setlength\parindent{0pt}
%\setlength\textwidth{1mm} % uncomment to get a very narrow measure

\begin{document}
\blurb
\end{document}

• why not \@macroa\discretionary{}{}{\hbox{, }}\@macrob ? – David Carlisle Sep 3 '19 at 14:00
• @DavidCarlisle - An excellent suggestion! :-) I've edited my answer to add some comments to mention this much simpler solution. – Mico Sep 3 '19 at 14:13
• @DavidCarlisle thanks, good idea! – Sora. Sep 3 '19 at 14:16