2

Consider the following example:

enter image description here

\documentclass{article}

\newcommand{\myspace}{\hspace{3em}}
\newcommand{\mycmd}{left\myspace right}

\begin{document}

1:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla rutrum urna sit amet sollicitudin.
$\mycmd$
Nulla gravida, leo nec finibus varius, turpis magna hendrerit mi, pellentesque tempus nisi lectus blandit sem. 

2:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla rutrum urna sit amet sollicitudin. 
Nulla gravida, leo nec finibus $\mycmd$ varius, turpis magna hendrerit mi, pellentesque tempus nisi lectus blandit sem. 

\renewcommand{\myspace}{$\hspace{3em}$}%
3:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla rutrum urna sit amet sollicitudin. 
Nulla gravida, leo nec finibus $\mycmd$ varius, turpis magna hendrerit mi, pellentesque tempus nisi lectus blandit sem. 

\end{document}

The space between left and right (content in math mode) in paragraph (1) is fixed at 3em. I want this space gobbled if it lands at the edge of the text block. Paragraph (2) highlights the problem, while paragraph (3) suggests a fix - switch out of math mode, insert the \hspace and switch back to math mode.

How can I create a fixed-width space in math mode that is gobbled at the end of a line?

My actual use-case is for smaller spaces (like `\,`).
3

Do you mean \allowbreak? Which is a wrapper for \penalty0; maybe, strict LaTeX code should do \linebreak[0].

\documentclass{article}

\newcommand{\myspace}{\hspace{3em}}
\newcommand{\mycmd}{left\myspace right}

\begin{document}

1:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla 
rutrum urna sit amet sollicitudin.
$\mycmd$
Nulla gravida, leo nec finibus varius, turpis magna hendrerit mi, pellentesque 
tempus nisi lectus blandit sem. 

2:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla 
rutrum urna sit amet sollicitudin. 
Nulla gravida, leo nec finibus $\mycmd$ varius, turpis magna hendrerit mi, 
pellentesque tempus nisi lectus blandit sem. 

\renewcommand{\myspace}{\allowbreak\hspace{3em}}%
3:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla 
rutrum urna sit amet sollicitudin. 
Nulla gravida, leo nec finibus $\mycmd$ varius, turpis magna hendrerit mi, 
pellentesque tempus nisi lectus blandit sem. 

\end{document}

enter image description here

TeX won't break at spaces in math mode, but it can at penalties (possibly automatically supplied).

1
  • The last sentence provides the best solution and clue. Thanks! – Werner Feb 11 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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