5

I've been struggling with nameref for some time now. I am trying to make them respect the available space there is on a line, but I can't seem to find how.

The LaTeX code is the following:

\documentclass{report}
%\documentclass{scrreprt}
\usepackage[inner=3.5cm,outer=2.5cm,top=2.5cm,bottom=2.5cm,pdftex]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[pdftex]{hyperref}

\hypersetup{
    colorlinks = true
}

\overfullrule=15pt % debug

\begin{document}

\chapter{Chapter name}

\section{Section name}

\subsection{XML\_DOM\_NodeFirstSonGet}\label{XMLDOMNodeFirstSonGet}
\subsection{XML\_DOM\_NodeSonsCount}\label{XMLDOMNodeSonsCount}
\subsection{XML\_DOM\_NodeSonsGet}\label{XMLDOMNodeSonsGet}
\subsection{XML\_DOM\_NodeSonsNodesCount}\label{XMLDOMNodeSonsNodesCount}
\subsection{XML\_DOM\_NodeSonsNodesCount}\label{XMLDOMNodeSonsNodesGet}

Normal text:\\\newline
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse\

See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsGet}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}.\
See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}.\

See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}, \nameref{XMLDOMNodeFirstSonGet}.
\end{document}

And the resulting PDF looks like this:

enter image description here

As you can see, the second and third line containing \namerefs are overfull and won't respect the line width as the normal text does.

The result is even worst with \documentclass{scrreprt} as they are all overfull and cropped by the page limit:

enter image description here

As I don't know the length of the string that will be displayed, I can't arbitrarily force a new line.

What also strikes me is that I didn't find any information about this behavior. Is there a way to fix this?

1
  • Welcome to TeX.sx! Your post was migrated here from another Stack Exchange site. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. Jan 28 '13 at 12:09
4

The paragraph structure for those containing \nameref is "bad" since TeX has no way of forcing a line-break within the unknown "words". As a possible correction, and to see how bad it may look if TeX didn't care so much about making things look pretty, add \sloppypar (or at least scope it within a group {...} around the affected paragraphs) before the \nameref paragraphs. Alternatives include using \raggedright or perhaps a breakable underscore (or combination of either):

enter image description here

enter image description here

enter image description here

enter image description here

Here's a complete example:

\documentclass{report}
%\documentclass{scrreprt}
\usepackage[inner=3.5cm,outer=2.5cm,top=1cm,bottom=1cm]{geometry}
\usepackage{lmodern}
\usepackage{hyperref}

\hypersetup{
    colorlinks = true
}

\overfullrule=15pt % debug

\begin{document}

\chapter{Chapter name}

\section{Section name}

\subsection{XML\textunderscore DOM\textunderscore NodeFirstSonGet}\label{XMLDOMNodeFirstSonGet}
\subsection{XML\textunderscore DOM\textunderscore NodeSonsCount}\label{XMLDOMNodeSonsCount}
\subsection{XML\textunderscore DOM\textunderscore NodeSonsGet}\label{XMLDOMNodeSonsGet}
\subsection{XML\textunderscore DOM\textunderscore NodeSonsNodesCount}\label{XMLDOMNodeSonsNodesCount}
\subsection{XML\textunderscore DOM\textunderscore NodeSonsNodesCount}\label{XMLDOMNodeSonsNodesGet}

Normal text:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse

Using \verb|\sloppypar|:

{\sloppypar
See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsGet}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}.

See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}.

See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}, \nameref{XMLDOMNodeFirstSonGet}.
}

\bigskip

Using \verb|\raggedright|:

{\raggedright
See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsGet}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}.

See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}.

See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}, \nameref{XMLDOMNodeFirstSonGet}.
}

\bigskip

Different \verb|\textunderscore|:

{\let\oldtextunderscore\textunderscore%
\renewcommand{\textunderscore}{\oldtextunderscore\hspace{0pt}}%
See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsGet}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}.

See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}.

See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}, \nameref{XMLDOMNodeFirstSonGet}.
}

Different \verb|\textunderscore| + \verb|\sloppypar|:

{\let\oldtextunderscore\textunderscore\sloppypar%
\renewcommand{\textunderscore}{\oldtextunderscore\hspace{0pt}}%
See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsGet}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}.

See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}.

See \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeFirstSonGet}, \nameref{XMLDOMNodeSonsCount}, \nameref{XMLDOMNodeSonsNodesCount}, \nameref{XMLDOMNodeSonsNodesGet}, \nameref{XMLDOMNodeFirstSonGet}.
}

\end{document}
1
  • That did the trick. I'll use the last one, '\textunderscore + \sloppypar'. Thanks a lot for your time Werner.
    – timetear
    Jan 27 '13 at 20:10

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.