How to allow line breaks in in-text formulas?

2,592 views
Skip to first unread message

berstel

unread,
Jun 17, 2008, 10:23:09 AM6/17/08
to LaTeX Users Group
Hi,

I have defined a new command as follows:
\newcommand{\ite}[3]{\mathsf{if}\,{#1}\,\mathsf{then}\,{#2}\,
\mathsf{else}\,{#3}}

The purpose of this command is to write formulas such as
\[ z = 1 + (\ite{x \ge 0}{x}{-x}) \]
that is:
z = 1 + (if x >= 0 then x else -x)

And if works ok. Now if I want to have such a formula as in-text, that
is, using $...$, I tend to get overfull hboxes, because the "if ...
then ... else ..." text is not cut.

Yet, when you write a formula such as $z = 2x + 3 \wedge y = 3z - 5x +
1$, LaTeX inserts line breaks as needed.

So my question is: how could I tell LaTeX, in the definition of my
command, that it may insert a line break just before the "then" or
just before the "else"?

Thanks in advance,
Bruno.

Werner Grundlingh

unread,
Jun 17, 2008, 3:53:36 PM6/17/08
to LaTeX Users Group
According to LaTeX's inline math 'experts':

LATEX can break an inline formula only when a relation symbol (=, >,
>, ...) or a binary operation symbol (+, -, ...) exists and at least
one of these symbols appears at the outer level of a formula. Thus $a+b
+c$ can be broken across lines, but ${a+b+c}$ not.

One step at correcting this, is possibly to define relational or
binary operators as your separators between the if/then/else
statements. Alternative, one can switch between inline math and text
mode in the following way:

\newcommand{\ite}[3]{\textsf{if}\ $#1$\ \textsf{then}\ $#2$\
\textsf{else}\ $#3$}

blah blah blah blah blah blah blah $z = 1 + ($\ite{x \ge 0}{x}{-x}$)$
blah blah blah blah blah blah blah

Although the above seems cumbersome, it does the job. You could also
change the spacing (currently {\ }) the way you want it (possibly
{\,}).

Hope this helps,
Werner
Message has been deleted

berstel

unread,
Jun 24, 2008, 10:34:08 AM6/24/08
to LaTeX Users Group
Thanks a lot for your help.

I redefined my \ite command from:
\newcommand{\ite}[3]{\mathsf{if}\,{#1}\,\mathsf{then}\,{#2}\,
\mathsf{else}\,{#3}}
to:
\newcommand{\ite}[3]{\mathsf{if}\:{#1}\mathrel{\mathsf{then}}
{#2}\mathrel{\mathsf{else}}{#3}}

That is, the "then" and "else" word are now considered as relational
operators. As a result, LaTeX may break the line just before of after
them, which is ok with me. As an additional benefit, LaTeX also puts a
correct amount of space around these words. This allowed me to remove
the "\," spacing instructions.

Alas it doesn't work for the word "if": when I surround "if" with
\mathrel, I get extra space before the "if". So I chose to use "\:",
which gives a good spacing. Anyway I wouldn't like a line break just
after the "if".

Regards,
Bruno
Reply all
Reply to author
Forward
0 new messages