16

A small example:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\tikz{\node[text width=1cm]{\tt totot\\t t t};}
\tikz{\node{\parbox{1cm}{\tt totot\\t t t}};}
\end{document}

result

As you can see, alignment is not respected in the first TikZ node but is right in the \parbox. Is it a bug of TikZ? Why the difference?

3
  • You can also use the following example, \begin{tikzpicture} \node[text width=1cm,font=\ttfamily]{t t t}; \node[font=\ttfamily] at (0,0.5) {t t t}; \end{tikzpicture} when you tell TikZ to respect line breaks via align or text width internally it switches from an \hbox to a minipage environment.
    – percusse
    Sep 11, 2013 at 10:13
  • but if i use a minipage instead of a parbox, it is still correctly aligned.
    – Xoff
    Sep 11, 2013 at 10:18
  • What did you use for width of the minipage?
    – percusse
    Sep 11, 2013 at 10:22

2 Answers 2

18

For some reason I can't really well understand, in a \node the parameters \spaceskip and \xspaceskip are set to non zero values; when \spaceskip is non zero, TeX uses it for the interword space instead of the default stored in the current font information.

Try the following example

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\tikz{\node[text width=3cm]{\ttfamily
totot\\
t t t\\
\the\spaceskip\ \the\xspaceskip\\
\spaceskip=0pt totot\\t t t};}
\end{document}

and see that setting \spaceskip=0pt cures the problem.

enter image description here

The option to set is text badly ragged (apparently TikZ doesn't like normal ragged right and adds to the LaTeX definition also the Plain TeX one when calling text ragged which is the default).

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\node[text width=1cm,font=\ttfamily,text badly ragged]{totot\\t t t};
\node[font=\ttfamily] at (1.5,0){\parbox{1cm}{totot\\t t t}};
\end{tikzpicture}
\end{document}

enter image description here

If an option for deciding about the \spaceskip doesn't exist in TikZ, it should.

4
  • Can it be the side effect of \nullfont?
    – percusse
    Sep 11, 2013 at 10:40
  • @percusse I don't think so; the \spaceskip must be explicitly set, in order it changes value. Maybe you can suggest a TikZ style option to set it to zero, rather than using that awful trick of mine.
    – egreg
    Sep 11, 2013 at 10:46
  • 1
    the key text justified sets \leftskip and \rightskip to 0pt and produces the desired effect. In general, the internal macro \tikz@text@action is executed just before the node contents and is where all the skip stuff is sorted out, so a new style could \def this macro. Sep 11, 2013 at 11:05
  • @MarkWibrow The problem is in the fact that TikZ uses Plain TeX's \raggedright rather than LaTeX's.
    – egreg
    Sep 11, 2013 at 11:41
5

In a parbox the text is justified and in a node with textwidth specified, the text is raggedright aligned. You can justify the text inside node.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{ragged2e}
\begin{document}
\tikz{\node[text width=1cm]{\justifying\tt totot\\t t t};}
\tikz{\node{\parbox{1cm}{\tt totot\\t t t}};}
\end{document}

enter image description here

Hence it is not a bug IMO.

Not related, but you can consider using \ttfamily instead of two letter \tt.

5
  • If I use raggedright in the parbox, it is still correctly aligned.
    – Xoff
    Sep 11, 2013 at 10:19
  • @Xoff: That, I don't know :-(
    – user11232
    Sep 11, 2013 at 10:35
  • It works on this example, but not on more complex ones. The only way to get it right is to have fixed width spaces.
    – Xoff
    Sep 11, 2013 at 10:36
  • @xoff can you show one such example?
    – user11232
    Sep 11, 2013 at 13:07
  • You're right, there is no such example, your answer is good but : I don't need \justifying, I just need to use package ragged2e. So I suspect this package redefine something that is used by tikz...
    – Xoff
    Sep 11, 2013 at 14:32

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.