tikz 中的零宽度文本节点

tikz 中的零宽度文本节点

我想创建一组将在tabular单元格中对齐的 tikz 图片。这些图片是彩色的水平条,在颜色变化的位置,我包含一个带有日期的文本节点。正如您在屏幕截图中看到的那样,当文本节点位于条的左边框时,它的宽度会添加到条的宽度中,当我尝试对齐时,我的条会被推到右侧:

在此处输入图片描述

这种现象发生在条 1、3 和 5 上,而条 2 和 4 正确对齐(左对齐)。我尝试使用(和\clap的双面版本),但没有成功。以下是获取颜色条的 tikz 代码:\llap\rlap

\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}}

然后使用代码如下:

\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}

我怎样才能告诉 tikz 在计算图片的全局宽度时不要考虑文本节点?

答案1

您可以使用overlay键从边界框中排除节点。不过,我个人可能会\path[use as bounding box] ...;使用

\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}

在此处输入图片描述

相关内容