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]}}

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!

  • 2
    It works for me if I replace \@macro1 (which is invalid) by \@macroa, and the same for the other two. Sep 3, 2019 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, 2019 at 14:07

2 Answers 2


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



\newcommand{\macroa}{Value of macro A}
\newcommand{\macrob}{Value of macro B}




enter image description here


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.

enter image description here

\usepackage[utf8]{inputenc} % 
\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]}}
%% 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'.

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

  • 4
    why not \@macroa\discretionary{}{}{\hbox{, }}\@macrob ? Sep 3, 2019 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, 2019 at 14:13
  • @DavidCarlisle thanks, good idea!
    – Sora.
    Sep 3, 2019 at 14:16

You must log in to answer this question.

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