# Inclusion of ragged2e causes spacing side-effect in Tikz

I am working on a memoir document whose chapter titles are styled with Tikz. My actual graphic is quite a bit more complex, but for the sake of simplicity, the MWEs in this question will simply consist of a diagonal red line (spanning the whole \textwidth) and the right-aligned chapter name.

In this document, I also have to use the ragged2e package (in order to achieve a hyphenated ragged-right alignment in a table). There mere action of including this package by means of \usepackage, however, seems to have the side-effect of changing the spacing in the Tikz graphic.

First, the MWE that works without any issues:

\documentclass{memoir}

\usepackage{tikz}
\usetikzlibrary{calc}

\renewcommand{\chaptermark}[1]{\markboth{\thechapter~#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection~#1}}
\pagestyle{plain}

\renewcommand{\printchaptername}{}
\renewcommand{\printchapternum}{}
\renewcommand{\afterchapternum}{}
\renewcommand{\printchaptertitle}[1]{\sffamily\Huge\begin{tikzpicture}
\clip (0,-.1) rectangle (\textwidth,1.1\baselineskip);

\draw[solid, draw=red] (0,0) -- (\textwidth,\baselineskip);

\node[align=right, anchor=center, text width=\textwidth] (headline) at ($(0,0)!0.5!(\textwidth,\baselineskip)$) {\thechapter~\textbf{#1}};
\end{tikzpicture}}

\begin{document}

\chapter{A chapter with spaces}

abc

\end{document}


When compiling this document, the chapter title looks like it should:

Now, the only change to the MWE is to include ragged2e:

\documentclass{memoir}

\usepackage{tikz}
\usetikzlibrary{calc}

\renewcommand{\chaptermark}[1]{\markboth{\thechapter~#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection~#1}}
\pagestyle{plain}

\renewcommand{\printchaptername}{}
\renewcommand{\printchapternum}{}
\renewcommand{\afterchapternum}{}
\renewcommand{\printchaptertitle}[1]{\sffamily\Huge\begin{tikzpicture}
\clip (0,-.1) rectangle (\textwidth,1.1\baselineskip);

\draw[solid, draw=red] (0,0) -- (\textwidth,\baselineskip);

\node[align=right, anchor=center, text width=\textwidth] (headline) at ($(0,0)!0.5!(\textwidth,\baselineskip)$) {\thechapter~\textbf{#1}};
\end{tikzpicture}}

\usepackage{ragged2e}

\begin{document}

\chapter{A chapter with spaces}

abc

\end{document}


Compiling now produces the chapter title as follows:

Obviously, the whitespace between each word within the chapter name has suddenly grown considerably. Interestingly, the space between the chapter number and the first word of the chapter name does not grow, even if the ~ is replaced with a .

How can I prevent this extra whitespace from happening?

Please do not suggestions that involve changing the Tikz code. As noted above, in the actual document, it is quite a complex graphic that has been carefully layouted. I simply want to use ragged2e without any side-effects by merely importing the package.

Curiously, my actual problem in my actual document is a bit different: There, both chapter titles and page headings are styled with Tikz. Chapter titles turn out normal, but chapter names in page headings (i.e. \leftmark) end up with these oversized whitespaces as soon as ragged2e is included. I was unable to reproduce this issue for an MWE, and instead managed to produce an MWE that exhibits the equivalent problem in the chapter titles. As this problem seems to be very similar to that in my actual document, I hope solutions for this question will also help with the actual issue.

• That is really strange. You can try this in the title node \parbox[t]{\textwidth}{\raggedleft \thechapter~\textbf{#1}} there is still a small difference with and without ragged2e but not as noticable – daleif Oct 15 '15 at 14:38

Use tikz ways of changing font attributes inside a node, i.e., instead of \textbf{#1} use font=\bfseries in the options. This will make the number bold too. There you may use \normalfont.

\node[align=right,  text width=\textwidth,font=\bfseries] (headline) at ($(0,0)!0.5!(\textwidth,\baselineskip)$) {{\normalfont\thechapter}~#1};


anchor=center is superfluous so I removed it.

\documentclass{memoir}

\usepackage{tikz}
\usetikzlibrary{calc}

\renewcommand{\chaptermark}[1]{\markboth{\thechapter~#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection~#1}}
\pagestyle{plain}

\renewcommand{\printchaptername}{}
\renewcommand{\printchapternum}{}
\renewcommand{\afterchapternum}{}
\renewcommand{\printchaptertitle}[1]{\sffamily\Huge\begin{tikzpicture}
\clip (0,-.1) rectangle (\textwidth,1.1\baselineskip);

\draw[solid, draw=red] (0,0) -- (\textwidth,\baselineskip);

\node[align=right,  text width=\textwidth,font=\bfseries] (headline) at ($(0,0)!0.5!(\textwidth,\baselineskip)$) {{\normalfont\thechapter}~#1};

\end{tikzpicture}}

\usepackage{ragged2e}

\begin{document}

\chapter{A chapter with spaces}

abc

\end{document}

• Fascinating, this seems to work. Mind to add a brief explanation of what is going on there behind the scenes (in case you know)? – O. R. Mapper Oct 15 '15 at 14:50
• @O.R.Mapper: It is some what more complex and low level. When you use align=right with text width and try to change the font attributes in the middle of the text, the ragged2e tries to implement TeX style raggedness where the left side is balanced to its best. So that spreading occurs. So don't try to change the attributes in the middle but well in advance as done by font=\bfseries. – user11232 Oct 15 '15 at 15:32
• @O.R.Mapper: There is another option in tikz: align=flush right which when used in your definition of node works well. Try \node[align=flush right,text width=\textwidth] (headline) at ($(0,0)!0.5!(\textwidth,\baselineskip)$) {\thechapter~\textbf{#1}}; with ragged2e to see what I mean. – user11232 Oct 15 '15 at 15:32
• @HarishKumar: This still to me looks like a bug in ragged2e. :-( – Reinstate Monica - M. Schröder Oct 16 '15 at 13:48