3

In the below MWE one can see that the section title runs into the margin of the page. This is due to the use of the \mbox{...}, but the \mbox is unfortunately in the normal case, automatically generated text, necessary.

Edit: The \mbox cannot be broken across lines, but is it possible to break the line before the \mbox?

How to prevent that the section title runs into the margin?

MWE:

\documentclass{book}

\usepackage{geometry}
\begin{document}

\chapter{Index}
\section{image more text even more format file caption \mbox{some more text even}}
image more text even some more text even more format file caption some more text even
image more text even some more text even more format file caption some more text even
image more text even some more text even more format file caption some more text even

\end{document}

The result:

enter image description here

4
  • 1
    It is sort of a feature of \mbox that it forms an unbreakable unit. As such it would be expected that it does not allow for line breaks (neither in section titles nor anywhere else). If you want to allow for line breaks I suggest you try to go to the source and avoid \mbox being used/generated. (Theoretically you could redefine what \mbox does, but I guess this could cause many more problems than it potentially solves. Redefining standard commands is rarely a good idea.)
    – moewe
    Feb 9, 2019 at 14:35
  • It is clear that \mbox cannot be broken, but would it be possible that the line-break takes place before the \mbox, I've edited this into the question.
    – albert
    Feb 9, 2019 at 14:39
  • 1
    Be aware that in case of using the hyperref-package, you might need \texorpdfstring or the like for adjusting the text that occurs in the bookmarks of your pdf-file. (The text in the bookmarks in the pdf-file will be processed by the pdf-viewing-program, not by LaTeX. The pdf-viewing-program will most likely not "understand" things like \mbox-commands .) But maybe you needn't care as \mbox is already one of the \pdfstringdefDisableCommands-thingies... Feb 9, 2019 at 15:06
  • In case the functionality of the \mbox is not needed but the \mbox-command is there due to automatic generation of text, you can probably within a local scope redefine the \mbox-command to just spit out the argument... When doing this be aware that \mbox-commands might also "slip" into moving arguments (entries of the table of contents, entries for \label-\nameref-references, page-headers etc) while a local redefinition of \mbox probably does not... Feb 9, 2019 at 15:17

1 Answer 1

4

Perhaps \sloppy and \raggedright are your friends.


\sloppy without \raggedright:

\documentclass{book}

%\usepackage{geometry}
\begin{document}

\chapter{Index}
\begingroup
\sloppy
%\raggedright
\section{image more text even more format file caption \mbox{some more text even}}\par
\endgroup
image more text even some more text even more format file caption some more text even
image more text even some more text even more format file caption some more text even
image more text even some more text even more format file caption some more text even

\end{document}

enter image description here


\sloppy and \raggedright:

\documentclass{book}

%\usepackage{geometry}
\begin{document}

\chapter{Index}
\begingroup
\sloppy
\raggedright
\section{image more text even more format file caption \mbox{some more text even}}\par
\endgroup
image more text even some more text even more format file caption some more text even
image more text even some more text even more format file caption some more text even
image more text even some more text even more format file caption some more text even

\end{document}

enter image description here

3
  • Looks promising, I'll do some tests later on with this on the original source.
    – albert
    Feb 9, 2019 at 15:03
  • Be aware that section headings are moving arguments. They do not occur only in the main text. They also appear in the table of contents and probably in page-headers and with pdf-files with hyperlinks they may appear in the bookmarks. They may also be delivered by \nameref. You may need to apply \sloppy (and \raggedright) to the table of contents / to other places where these section headings occur, as well... Perhaps you can convince your automatic text generator not to deliver \mbox but something like\mycmd so you can in any place locally redefine \mycmd according to your needs. Feb 9, 2019 at 15:25
  • Unless you remove the formatting in the optional argument (short title). Feb 9, 2019 at 17:23

You must log in to answer this question.

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