Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I've been fiddling with TikZ nodes to get framed text (part of mdframed boxes, but that's beside the point) when I discovered something that puzzles me. If you run the code below:

\documentclass{article}
\usepackage{tikz}
\begin{document}
with \verb|TikZ|:

\tikz{\node[draw]{t};}

\tikz{\node[draw]{t~};}

\tikz{\node[draw]{t~~};}

with \verb|\framebox|:

\framebox{t}

\framebox{t~}
\end{document}

you get:

I keep wondering where the first space (~) behind the t goes? On the contrary, \framebox seems to respect this mandatory space.

What's happening here?

share|improve this question
1  
I got the same effect with ~ replaced by \ –  Matthew Leingang Apr 24 '13 at 17:09
    
Maybe related to the "\par eats glue" discovery in this answer? –  Matthew Leingang Apr 24 '13 at 17:11
1  
@MatthewLeingang Not \par eats glue: \unskip eats glue. –  David Carlisle Apr 24 '13 at 17:12
    
TikZ removes spaces to save you from writing % all over the place. Is there a real use-case scenario behind this question? –  Qrrbrbirlbel Apr 24 '13 at 17:15
    
@Qrrbrbirlbel: There is: I've been recreating (with modifications) the fancy example from mdframed with TikZ with the boxed title for the frame. If you look, even there, the title is followed by one ~ to have a nice spacing, but this results in nothing. Finally you end up with a wider space at the beginning of the title than at its end. Not really a beauty to behold (especially with larger font size). –  Count Zero Apr 24 '13 at 17:20
show 1 more comment

1 Answer 1

up vote 9 down vote accepted

When in doubt add \tracingall. If you modify your input to say

{\tracingall\tikz{\node[draw]{t~};}}

For the second case you get the log below,

~->\nobreakspace {}

\nobreakspace ->\protect \nobreakspace  
{\relax}

\nobreakspace  ->\leavevmode \nobreak \ 

\leavevmode ->\unhbox \voidb@x 
{\unhbox}

\nobreak ->\penalty \@M 
{\penalty}
{\ }
{begin-group character {}
{end-group character }}
{end-group character }}
{restoring \tikz@align@header=undefined}
{restoring \graph=macro:->\path graph}
{restoring \datavisualization=macro:->\tikz@lib@datavisualization }
{restoring \calendar=macro:->\tikz@lib@cal@calendar }
{restoring \matrix=macro:->\tikz@path@overlay {node[matri\ETC.}
{restoring \coordinate=macro:->\tikz@path@overlay {coordinate\ETC.}
{restoring \node=macro:->\tikz@path@overlay {node}}
{restoring \useasboundingbox=macro:->\path [use as bounding box]}
{restoring \clip=macro:->\path [clip]}
{restoring \shadedraw=macro:->\path [shade,draw]}
{restoring \shade=macro:->\path [shade]}
{restoring \filldraw=macro:->\path [fill,draw]}
{restoring \fill=macro:->\path [fill]}
{restoring \pattern=macro:->\path [pattern]}
{restoring \draw=macro:->\path [draw]}
{restoring \againpath=macro:#1->\pgfextra {\pgfsyssoftpath@getcurrentpath \ETC.
}
{restoring \path=macro:->\let \tikz@signal@path =\tikz@signal@path \ETC.}
{restoring \stopscope=macro:->\tikz@atend@scope \iftikz@transparency@group \ETC
.}
{restoring \startscope=macro:->\pgfutil@ifnextchar [\tikz@@scope@env \ETC.}
{restoring \endscope=macro:->\tikz@atend@scope \iftikz@transparency@group \ETC.
}
{restoring \scope=macro:->\pgfutil@ifnextchar [\tikz@@scope@env \ETC.}
{restoring \pgfsysprotocol@temp=macro:->{q }}
{restoring \pgf@colnext=\relax}
{restoring \pgf@coltest=macro:->}
{restoring \pgf@colmarshal=macro:->\pgfsys@color@gray {0}}
{restoring \\color@pgf@tempcolor=macro:->\xcolor@ {}{0 g 0 G}{gray}{0}}
{restoring \reserved@c=macro:->\XC@definec@lor []{pgf@tempcol\ETC.}
{restoring \@let@token=the letter n}
{restoring \reserved@b=macro:->\XC@definec@lor []{pgf@tempcol\ETC.}
{restoring \reserved@a=macro:->\XC@definec@lor []{pgf@tempcol\ETC.}
{\unskip}

Which shows that after expanding ~ Tikz ends a group (and so TeX restores a load of definitions) and the the unskip primitive (from \unskip) removes the space.

This is similar to the LaTeX definition of tabular cells which use \unskip to remove trailing spaces in the content but LaTeX box commands do not do this. Note \unskip only removes one skip so ~~ the second one is removed but the first remains.

share|improve this answer
    
Ok, so this means I just have to add two ~s if I want to addend a visible space? –  Count Zero Apr 24 '13 at 17:17
    
@CountZero Try this one \node (a) at (0,0) {A \ \ \ \ \ \ A}; –  percusse Apr 24 '13 at 17:18
    
Baaah I can't put more spaces in between. Fortunately TikZ does the same :). Consecutive spaces are eaten. –  percusse Apr 24 '13 at 17:19
1  
@CountZero You can protect yourself from \unskip by using ~\hbox{} or hbox{~} or ~\kern\z@ according to taste. –  David Carlisle Apr 24 '13 at 17:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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