TransWikia.com

Forbidding line breaks after specific symbols in inline math?

TeX - LaTeX Asked by imakhlin on May 31, 2021

I’m writing a text in which order relations (e.g. <, prec, ll) often appear as arguments of functions rather than binary operators (e.g. $O(P,<)$). Every so often this leads to an inline formula’s line breaking after the order relation and leaving a closing bracket or a comma (when there’s another argument after the order relation) at the beginning of the next line. This, of course, I’d like to avoid.

I’m hoping for a more flexible solution than relpenalty=10000 because I don’t like it doing way more than is necessary and possibly messing with the way other formulas are typeset. I only really want to forbid line breaks after the symbols <, prec, ll as well as these symbols with a subscript or superscript added. However, I can’t find a way to adjust the penalty for breaking after a specific symbol. Perhaps, there’s a way of doing this by changing the mathcode of said symbols but I can’t put my finger on how exactly that could work.

Update. To be absolutely clear, I do not want to forbid the line break ad hoc at every instance but I want to do it a the preamble level. Also, if there is a way of forbidding line breaks before closing brackets and commas that would also work (perhaps that would even be better, although it wouldn’t match the question’s title, lol).

3 Answers

This will prohibit line breaks after every occurrence of <, prec and ll, not just in the given context. You may add penaltyrelpenalty in case you have problems with line breaking in other contexts.

documentclass{article}
usepackage{amsmath}

% save the necessary commands and codes
mathchardefimakhlinless=mathcode`<
mathchardefimakhlinprec=prec
mathchardefimakhlinll=ll

% define a generic not breaking combination
% accepting subscripts and superscripts before
% issuing nobreak
NewDocumentCommand{nobreaking}{me{^_}}{%
  #1IfValueT{#2}{^{#2}}IfValueT{#3}{_{#3}}nobreak
}

% renew the desired commands
renewcommand{prec}{nobreakingimakhlinprec}
renewcommand{ll}{nobreakingimakhlinll}
newcommand{aliasless}{nobreakingimakhlinless}
begingrouplccode`~=`< lowercase{endgrouplet~}aliasless
mathcode`<="8000

begin{document}

parbox{0pt}{$O(P,<)$
$O(P,<_1)$
$O(P,ll)$
$O(P,prec_1^2)$
}

end{document}

The parbox with zero width is meant to force all possible line breaks.

enter image description here

Correct answer by egreg on May 31, 2021

This is partially inspired by the comments of barbara beeton. One can actually use mathcode to change the symbol class of <. I added the following to my preamble, it does the trick.

mathcode`<="013C
letrelprecprec
renewcommand{prec}{{relprec}}
letrelllll
renewcommand{ll}{{relll}}

Here in the last 4 lines I simply redefine prec to mean {prec} and ll to mean {ll}, so that after these commands lines break as they do after Ord (class 0) symbols rather than Rel (class 3) symbols. This, however, does not work for < for the obvious reasons and instead I change its class to 0 via mathcode in the first line. In 013C the first digit 0 is the new class, 1 is the cursive font (I can't use the usual family 0, because this leads to < being typeset as a "¡", fortunately, using cursive does not affect the shape of "<") and 3C is the character code of "<".

Of course, it is also possible to use mathcode`<="8000 for a more conceptual approach, as is done in egreg's answer. Also, as someone pointed out in a deleted comment, changing the class will mess with the spacing around the symbols when I actually use them as infix binary operators, although I can't say I mind too much because I always feel like these spaces are too big.

Answered by imakhlin on May 31, 2021

An alternative approach is the one I mentioned in the update: forbidding line breaks before commas and closing brackets. This is done by

mathchardefnewbracket=mathcode`)
mathcode`)="8000
begingroup
catcode`) active
gdef){nolinebreaknewbracket}
endgroup

mathchardefnewcomma=mathcode`,
mathcode`,="8000
begingroup
catcode`, active
gdef,{nolinebreaknewcomma}
endgroup

The upside of this solution for me personally is that it doesn't create difficulties with subscripts, since I do use _< and _prec but not _) or _,. It also keeps the spacing as it is, unlike my other answer.

Of course, as I pointed out, this solves my problem but doesn't answer the precise question in the title. Also, I'm nowhere near as TeXnologically literate as most people who post answers here, so please let me know if there's something grossly inappropriate about the above code.

Answered by imakhlin on May 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP