1

I would like to create a bunch of tikz pictures that will be aligned in tabular cells. These pictures are colored horizontal bars and at the location of color change I include a text node with a date. As you can see on the screen capture, when the text node is on the left border of the bar, its width is added to the width of the bar and when I try to align, my bar is pushed to the right:

enter image description here

This phenomenon occurs on bars 1, 3 and 5, while bars 2 and 4 are correctly aligned (flush left). I tried to use a \clap (the both-sides version of \llap and \rlap) but it didn't worked. Here is the tikz code to obtain the color bar:

\def\charly#1#2#3#4{\begin{tikzpicture}[fill=white,every path/.style={draw},scale=0.5]
\fill (0,0) rectangle (10,0.2);
\fill[color=red] (#1,0) rectangle (#2,0.2);
\fill[color=blue] (#2,0) rectangle (10,0.2);
\node (A) at (#1,-0.2) { \fontsize{6pt}{5pt}\selectfont\clap{#3} };
\node (B) at (#2,-0.2) { \fontsize{6pt}{5pt}\selectfont\clap{#4} };
\end{tikzpicture}}

and the code is then used as follows:

\begin{tabular}{lll}
Error&Correct&Interval of error occurrences\\\hline
dont (138)&don't (2)&\charly{0}{9.72972972972973}{3/3}{4/8}\\
dint (45)&didn't (2)&\charly{0.27027027027027}{9.72972972972973}{3/4}{4/8}\\
tolld (39)&told (43)&\charly{0}{1.08108108108108}{3/3}{3/7}\\
pepul (37)&people (10)&\charly{0.27027027027027}{1.08108108108108}{3/4}{3/7}\\
werk (31)&work (11)&\charly{0}{4.86486486486486}{3/3}{3/21}\\
\end{tabular}

How can I tell tikz not to take the text node into account when calculating the global width of the picture?

2
  • 2
    Try \node[overlay] .... You can also use \path[use as bounding box] <some universal path>;` to have all bars share the same bounding box.
    – user194703
    Jun 14, 2020 at 8:57
  • The overlay tip worked, thank you so much! This one of those simple things you can search for hours in the manual…
    – yannis
    Jun 14, 2020 at 9:03

1 Answer 1

2

You can use the overlay key to exclude the node(s) form the bounding box. However, I personally would probably use \path[use as bounding box] ...; as in

\documentclass{article}
\usepackage{tikz}
\newcommand\charly[4]{\begin{tikzpicture}[fill=white,
    every path/.style={draw},scale=0.5,
    node font=\fontsize{6pt}{5pt}\selectfont
    ]
\path[use as bounding box,draw=none]    (-0.2,-0.2) rectangle (10,0.2);
\fill (0,0) rectangle (10,0.2);
\fill[color=red] (#1,0) rectangle (#2,0.2);
\fill[color=blue] (#2,0) rectangle (10,0.2);
\node (A) at (#1,-0.2) {#3};
\node (B) at (#2,-0.2) {#4};
\end{tikzpicture}}
\begin{document}

\begin{tabular}{lll}
Error&Correct&Interval of error occurrences\\\hline
dont (138)&don't (2)&\charly{0}{9.72972972972973}{3/3}{4/8}\\
dint (45)&didn't (2)&\charly{0.27027027027027}{9.72972972972973}{3/4}{4/8}\\
tolld (39)&told (43)&\charly{0}{1.08108108108108}{3/3}{3/7}\\
pepul (37)&people (10)&\charly{0.27027027027027}{1.08108108108108}{3/4}{3/7}\\
werk (31)&work (11)&\charly{0}{4.86486486486486}{3/3}{3/21}\\
\end{tabular}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .