tikzpicture 不适合表格,因为对象内部有“?”符号

tikzpicture 不适合表格,因为对象内部有“?”符号

我试图让图表中的表格看起来不错,但对象\tolabel似乎溢出了圆的半径,这使得表格看起来不对齐。

输出

如果我将最后一个对象换成另一个对象,例如\labela\labelb它适合。这似乎是由于圆圈内的问号。我该如何让它适合?

此表是包含其他组件的更大图形的一部分。但这是唯一不起作用的部分。我的代码如下:

\documentclass{article}
\usepackage{graphicx} % Required for inserting images

\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, calc}

    \newcommand{\labela}{\tikz{\fill[blue, draw=black] (0,0) circle (0.25cm);}}
    \newcommand{\labelb}{\tikz{\fill[purple, draw=black] (0,0) circle (0.25cm);}}
    \newcommand{\unlabeled}{\tikz{\fill[white, draw=black] (0,0) circle (0.25cm);}}
    \newcommand{\tolabel}{%
    \tikz{\fill[yellow, draw=black] (0,0) circle (0.25cm); \node[text=black] at (0,0) {\textbf{?}};}%
}

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}
        \node[draw, fill=gray!20, minimum width=3cm, minimum height=1cm] at (15,4) {%
            \begin{tabular}{@{}l@{}}
                Legend \\
                \labela \hspace{0.5em} Label A \\
                \labelb \hspace{0.5em} Label B \\
                \unlabeled \hspace{0.5em} Unlabeled \\
                \tolabel \hspace{0.5em} {\shortstack{Sample to\\ be labeled}}
            \end{tabular}
        };
    \end{tikzpicture}
    \caption{Caption}
    \label{fig:enter-label}
\end{figure}

\end{document}

答案1

这显示了如何使用保存框而不是 tikzpictures。我还移至基线以匹配文本,

\documentclass{article}
\usepackage{graphicx} % Required for inserting images

\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, calc}

    \newsavebox{\labela}
    \savebox{\labela}{\tikz[baseline=(A.base)]{\fill[blue, draw=black] (0,0) circle (0.25cm);
      \node (A) at (0,0) {\textbf{\strut}};}}
    \newsavebox{\labelb}
    \savebox{\labelb}{\tikz[baseline=(A.base)]{\fill[purple, draw=black] (0,0) circle (0.25cm);
      \node (A) at (0,0) {\textbf{\strut}};}}
    \newsavebox{\unlabeled}
    \savebox{\unlabeled}{\tikz[baseline=(A.base)]{\fill[white, draw=black] (0,0) circle (0.25cm);
      \node (A) at (0,0) {\textbf{\strut}};}}
    \newsavebox{\tolabel}
    \savebox{\tolabel}{\tikz[baseline=(A.base)]{\fill[yellow, draw=black] (0,0) circle (0.25cm); 
      \node (A) at (0,0) {\textbf{?}};}}

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}
        \node[draw, fill=gray!20, minimum width=3cm, minimum height=1cm] at (15,4) {%
            \begin{tabular}{@{}l@{}}
                Legend \\
                \usebox\labela \hspace{0.5em} Label A \\
                \usebox\labelb \hspace{0.5em} Label B \\
                \usebox\unlabeled \hspace{0.5em} Unlabeled \\
                \usebox\tolabel \hspace{0.5em} {\shortstack{Sample to\\ be labeled}}
            \end{tabular}
        };
    \end{tikzpicture}
    \caption{Caption}
    \label{fig:enter-label}
\end{figure}

\end{document}

演示

答案2

这是 TikZ 矩阵的工作:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
    mynode/.style={
      circle, 
      text height=2.5ex, text depth=1ex, 
      text width=.25cm, 
      inner sep=0pt, 
      text centered,
      draw, 
      fill=#1
      },
    }

\usepackage{caption}

\begin{document}
\begin{figure}\centering
\begin{tikzpicture} 
\matrix[
  matrix of nodes, nodes in empty cells,
  fill=gray!30,
  nodes={anchor=center},
  column 2/.style={text width=width("be labelled")},
  inner xsep=6pt,
  row sep=4pt,
  ] (mymatr) {  
   &[4pt] \\[6pt]
  |[mynode=blue]| & Label A \\
  |[mynode=purple]| & Label B \\
  |[mynode=white]| & Unlabelled \\[-2pt]
  |[mynode=yellow]|{?} & {Sample to\\ be labelled} \\
  };
  \node[anchor=north west, inner ysep=4pt] at (mymatr.north west) {Legend};
  \end{tikzpicture}
\caption{Caption}
\end{figure}
\end{document}

在此处输入图片描述

相关内容