5

I've used the following SE answer to try to center the text in the left half or right half of a node. The left half is placed OK, but the right half is acting weird, and I haven't figured out why.

MWE:

\documentclass[tikz,border=1cm]{standalone}
\usepackage{standalone}
\usepackage{tikz}
\begin{document}
\newcommand{\textlabel}[3]{\node[label, inner xsep=\LabelSize/2, fill=#2] at (0,2*#1) {#3};}

\newcommand{\textlefthalf}[2]         {\textlabel{#1}{#2}{\hspace*{-\LabelSize}1}}
\newcommand{\textrighthalf}[2]        {\textlabel{#1}{#2}{\hspace*{\LabelSize}1}}
\newcommand{\textrighthalfalt}[2]     {\textlabel{#1}{#2}{1\hspace*{-\LabelSize}}}
\newcommand{\textrighthalfaltcolor}[2]{\textlabel{#1}{#2}{\color{white}1\hspace*{-\LabelSize}}}


\newlength\LabelSize
\setlength\LabelSize{1cm}

\tikzset{
    label/.style={fill=black,text width=\LabelSize, align=center,minimum height=\LabelSize,minimum width=\LabelSize*2,inner sep=0em,text=white,font=\sffamily\fontsize{15pt}{0pt}\selectfont},
}    
\begin{tikzpicture}
\textlefthalf{0}{blue}
\textrighthalf{1}{red}
\textrighthalfalt{2}{black}
\textrighthalfaltcolor{3}{gray}
\draw[dashed] (0,-1) -- (0,7);
\draw[green, dashed] (-\LabelSize/2,-1) -- (-\LabelSize/2,7);
\draw[green, dashed] (\LabelSize/2,-1) -- (\LabelSize/2,7);    
\end{tikzpicture}

\end{document}

enter image description here

Blue is okay, right is off-'centre', black is centered to the real center (not the one I intend). The gray one is the one I want to see. But why does adding the \color{white} help to achieve this?

I'd like to understand what's happening so that I can tune my mental model of latex.

  • mirrors.ctan.org/macros/latex/required/graphics/color.pdf section 3.2 shows that a side-effect of \color is that it forces \leavevmode which might be one of the reasons that this 'fixes' it. Still doesn't explain why the red one is half off/half on target. – Joost Jul 31 '18 at 21:09
  • 2
    Are you interested in finding out why that happens or in an alternative solution that does not play with \hspace? – user121799 Jul 31 '18 at 21:14
  • For this particular question I would like to know why this happens. But I am also open to suggestions on alternative approaches. – Joost Jul 31 '18 at 21:17
5

If you look carefully, you'll see that the "not working" part is the only one containing a positive \hspace. You have set text width=\LabelSize and put a horizontal space of the same width in it. So, the box is full: everything after that space, runs out of the box. I used a \rule to show that.

Instead, using negative spaces to position boxes can work quite well, as you have seen, but one has to carefully figure out, what you actually are doing. To properly get what you want, you can use another negative space, but in order to show its effect, there has to be any box after it. Else this space is not recognized. So I put a \null there:

\documentclass[tikz,border=1cm]{standalone}
\usepackage{standalone}
\usepackage{tikz}
\begin{document}
\newcommand{\textlabel}[3]{\node[label, inner xsep=\LabelSize/2, fill=#2] at (0,2*#1) {#3};}

\newcommand{\textlefthalf}[2]    {\textlabel{#1}{#2}{\hspace*{-\LabelSize}1}}
\newcommand{\textrighthalf}[2]   {\textlabel{#1}{#2}{\rule{\LabelSize}{1pt}1}}
\newcommand{\textrighthalfalt}[2]{\textlabel{#1}{#2}{1\hspace*{-\LabelSize}\null}}

\newlength\LabelSize
\setlength\LabelSize{1cm}

\tikzset{
    label/.style={fill=black,text width=\LabelSize, align=center,minimum height=\LabelSize,minimum width=\LabelSize*2,inner sep=0em,text=white,font=\sffamily\fontsize{15pt}{0pt}\selectfont},
}    
\begin{tikzpicture}
\textlefthalf{0}{blue}
\textrighthalf{1}{red}
\textrighthalfalt{2}{black}
\draw[dashed] (0,-1) -- (0,5);
\draw[green, dashed] (-\LabelSize/2,-1) -- (-\LabelSize/2,5);
\draw[green, dashed] (\LabelSize/2,-1) -- (\LabelSize/2,5);    
\end{tikzpicture}

\end{document}

result

  • "but in order to show its effect, there has to be any box after it. " I don't see why. It must be because of how latex works internally, but I just haven't seen any explanation to as why this must be the case...the \color is put in front so your explanation is missing one small step. Perhaps the change back to the default color causes this? – Joost Aug 1 '18 at 3:11
  • 1
    If I remember correctly, I read about this behavior in the TeXbook by Donald Knuth. So yes, this is a tex internal. Why it works with \color though, or what it actually changes, I don't know to be honest. – nox Aug 1 '18 at 9:40
4

This is not an answer to your question but an alternative method to achieve what you seem to want. (The shapes.multiparts is not essential but may be useful in case you want to access the various parts.) Of course, one could write macros for these things.

\documentclass[tikz,border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\begin{document}
\newlength\LabelSize
\setlength\LabelSize{1cm}

\begin{tikzpicture}[font=\sffamily\fontsize{15pt}{0pt}\selectfont]
\node[rectangle split, rectangle split parts=2,rectangle split
horizontal,fill=blue,text=white,rectangle split part align={center,center},
inner sep=0pt,minimum height=1cm] 
at (0,0) {\makebox[1cm][c]{1}\nodepart{two}\makebox[1cm][c]{}}; 
\node[rectangle split, rectangle split parts=2,rectangle split
horizontal,fill=red,text=white,rectangle split part align={center,center},
inner sep=0pt,minimum height=1cm] 
at (0,2) {\makebox[1cm][c]{}\nodepart{two}\makebox[1cm][c]{1}}; 
\node[fill=black,text=white,minimum width=2*\LabelSize,
inner sep=0pt,minimum height=1cm] 
at (0,4) {1}; 
\draw[dashed] (0,-1) -- (0,5);
\draw[green, dashed] (-\LabelSize/2,-1) -- (-\LabelSize/2,5);
\draw[green, dashed] (\LabelSize/2,-1) -- (\LabelSize/2,5);    
\end{tikzpicture}
\end{document}

enter image description here

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.