Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sometimes I have to work with long formulas that can (or I want) not be split over two lines. I'm trying to write a macro that sent the formula at the new line if the formula would go over the margin. For example, with lines of twenty characters, and calling \Foo this macro: a text like

12345678901234567890
aaaaa aaaa aaaaaa $bbbbbb$
aaaaa aaaa

is formatted as

12345678901234567890
aaaaa aaaa aaaaaa bbbbbb
aaaaa aaaa

while

12345678901234567890
aaaaa aaaa aaaaaa \Foo{$bbbbbb$}
aaaaa aaaa

is formatted as

12345678901234567890
aaaaa aaaa aaaaaa
bbbbbb aaaaa aaaa

Since this require to verify if such macro is over the limit after printing, I suppose is something requiring multiple step of compilation. The only way I know to retrieve coordinates of a point is using a tikz coordinate, but so a "pseudocode" draft of what I'm trying to do is:

\newcommand{\Foo}[1]{%
  \myCount=\myCount+1
  \if\NameConcat\FooVar\myCount>\pageLength\endline\else\relax
  #1%
  \coordinate\NameConcat\FooVar\myCount%
  }

(at the start \myCount and all \FooVars have to be "zero", and \NameConcat concatenate the FooVar name and the counter creating as many counter as needed)

So, what is the best way to implement this in LaTeX?

share|improve this question
add comment

1 Answer

I wouldn't do it so complicated. You can just put the formula or any other text in a \mbox{...} to prevent it from being broken across lines:

12345678901234567890
aaaaa aaaa aaaaaa \mbox{$bbbbbb$}
aaaaa aaaa 
share|improve this answer
    
NO, if I do so I obtain that it goes OUTSIDE the maximum line size, out of the border of the page/coloumn. What I want is to automatically go on a new line in that case. –  marco Apr 29 '11 at 10:02
    
@macro: That's not always the case but depends on the line content. You can influence this by modifying some penalties. You can also put \linebreak[3] in front of \mbox, best as part of a macro, to pursue LaTeX to break the line there if required. –  Martin Scharrer Apr 29 '11 at 10:11
    
Sorry for the long time for the reply. It is not working either. Below there is a better explanation. –  marco May 17 '11 at 8:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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