如何在表格内(或)外对齐两个饼图

如何在表格内(或)外对齐两个饼图

在一份将以 (白色/黑色) 打印的报告中,我想添加两个饼图来显示已在两个实验中使用的样本大小。我不知道如何对齐它们,而且对我来说使用起来相当困难 (基线),所以我想在 中执行此操作,然后我添加了一个表格只是为了添加标题。问题是这些图表的对齐方式不太专业。正如您在下表中看到的那样,

输出表

我用来生成该代码的代码如下:

\usepackage{calc}
\usepackage{ifthen}
\usepackage{tikz}

\newcommand{\slice}[4]{
\pgfmathparse{0.5*#1+0.5*#2}
\let\midangle\pgfmathresult

 % slice
 \draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;

 % outer label
 \node[label=\midangle:#4] at (\midangle:1) {};

 % inner label
 \pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
 \let\temp\pgfmathresult
 \pgfmathparse{max(\temp,-0.5) + 0.8}
 \let\innerpos\pgfmathresult
 \node at (\midangle:\innerpos) {#3};
 }
     \begin{table}[]
     \centering
     \begin{tabular}{|l|l|}
     \hline
          \begin{tikzpicture}[scale=2.2]

          \newcounter{a}
          \newcounter{b}
          \foreach \p/\t in {23/AbSn, 59/BrJH, 14/CoJN,
               4/DiH2}
             {
              \setcounter{a}{\value{b}}
              \addtocounter{b}{\p}
              \slice{\thea/100*360}
              {\theb/100*360}
              {\p\%}{\t}
             }

         \end{tikzpicture}

         & 
         \begin{tikzpicture}[scale=2.2]

         \newcounter{a}
         \newcounter{b}
         \foreach \p/\t in {56/AbSn, 2/BrJH, 31/CoJN,
               11/DiH2}
                {
                  \setcounter{a}{\value{b}}
                  \addtocounter{b}{\p}
                  \slice{\thea/100*360}
                  {\theb/100*360}
                  {\p\%}{\t}
                 }

      \end{tikzpicture}

\\ \hline
\end{tabular}
\caption{The quantity of given samples}
\label{
samples}

\end{table}

答案1

像这样?

在此处输入图片描述

将 s括tikzpicture在两个表格内,tikzpictures 通常在基线上构建,而表格自然垂直居中在基线上。除此之外,计数器ab被定义了两次。

\documentclass[12pt,a4paper]{article}
\usepackage{calc,array}
\usepackage{ifthen}
\usepackage{tikz}
\usepackage[margin=1in]{geometry}
\begin{document}

\newcommand{\slice}[4]{
\pgfmathparse{0.5*#1+0.5*#2}
\let\midangle\pgfmathresult
 % slice
 \draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;
 % outer label
 \node[label=\midangle:#4] at (\midangle:1) {};
 % inner label
 \pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
 \let\temp\pgfmathresult
 \pgfmathparse{max(\temp,-0.5) + 0.8}
 \let\innerpos\pgfmathresult
 \node at (\midangle:\innerpos) {#3};
 }
     \begin{table}
     \centering
     \begin{tabular}{|l|l|}
     \hline
     \begin{tabular}{@{}c@{}}
          \begin{tikzpicture}[scale=2.2]
          \newcounter{a}
          \newcounter{b}
          \foreach \p/\t in {23/AbSn, 59/BrJH, 14/CoJN,
               4/DiH2}
             {
              \setcounter{a}{\value{b}}
              \addtocounter{b}{\p}
              \slice{\thea/100*360}
              {\theb/100*360}
              {\p\%}{\t}
             }
         \end{tikzpicture}
         \end{tabular}
         & \begin{tabular}{@{}c@{}}%
         \begin{tikzpicture}[scale=2.2]
         \foreach \p/\t in {56/AbSn, 2/BrJH, 31/CoJN,
               11/DiH2}
                {
                  \setcounter{a}{\value{b}}
                  \addtocounter{b}{\p}
                  \slice{\thea/100*360}
                  {\theb/100*360}
                  {\p\%}{\t}
                 }
      \end{tikzpicture}
      \end{tabular}
\\ \hline
\end{tabular}
\caption{The quantity of given samples}
\label{samples}
\end{table}

\end{document}

相关内容