如何将 circuitikz 组件垂直置于表格中央

如何将 circuitikz 组件垂直置于表格中央

我需要制作一个电路图组件表。组件用 绘制circuitikz。这是我尝试使用的代码。\renewcommand{arraystretch}{2.5}此处注释掉 ,我用它来尝试获得一些垂直填充。代码生成的输出(有和没有 arraystretch 设置)如下所示。

\documentclass[11pt]{article}
\usepackage{circuitikz}

\begin{document}
%\renewcommand{\arraystretch}{2.5}

\begin{tabular}{|c|c|}
  \hline
  too high &
             \begin{circuitikz}
               \draw (0,0) to [lamp] (2,0);
             \end{circuitikz} 
\\ \hline 
  this one is centred vertically   &
                                     \begin{circuitikz} 
                                       \draw (0,0) to [european resistor] (2,0);
                                     \end{circuitikz} 
\\ \hline
  but this on is too high &
                            \begin{circuitikz} 
                              \draw (0,0) to [empty diode] (2,0);
                            \end{circuitikz} 
\\ \hline
\end{tabular}
\end{document}

使用 arraystretch 无任何伸展力

我如何创建这样一个表格,其中组件的图表垂直居中,并且在它们各自的表格单元格内有一些垂直填充(最好不需要手动调整每个图表的位置)?

答案1

小修改Phelype Oleinik 的答案。添加的是cellspace用于在单元格内容上方和下方添加垂直空间的包:

\documentclass[11pt]{article}
\usepackage{circuitikz}
\usepackage{array,cellspace}
\setlength\cellspacetoplimit{4pt}    % can be placed before "\begin{tabular}"
\setlength\cellspacebottomlimit{4pt}

\begin{document}
\begin{table}[htb]
\tikzset{baseline={([yshift=-0.5ex]current bounding box.center)}}% fixed position of current bounding box
    \begin{tabular}{|Sc|Sc|}
  \hline
too high
    &   \begin{circuitikz}
        \draw (0,0) to [lamp] (2,0);
        \end{circuitikz}                \\
    \hline
this one is centred vertically
    &   \begin{circuitikz}
        \draw (0,0) to [european resistor] (0,2);
        \end{circuitikz}                \\
    \hline
but this on is too high
    &   \begin{circuitikz}
        \draw (0,0) to [empty diode,anchor=west] (2,0);
    \end{circuitikz}                    \\
    \hline
    \end{tabular}
\end{table}
\end{document}

在此处输入图片描述

答案2

这里的问题是 TiZ 将图片的底部与文本的基线对齐。您可以使用以下命令更改绘图的基线baseline option

在此处输入图片描述

baseline=(current bounding box.center)文本的基线与图片的中心对齐。为了使它看起来更居中,您可以添加一个yshift将图片中心的宽度与文本的中心对齐。在我看来yshift=-.6ex看起来不错。

您可以将此设置放在\tikzset组中。一旦该组结束,设置将恢复。如果您将此设置放在组中,\begin{table}它将起作用。

\documentclass[11pt]{article}
\usepackage{circuitikz}

\begin{document}

{%
\tikzset{baseline={([yshift=-.6ex]current bounding box.center)}}
\begin{tabular}{|c|c|}
  \hline
  too high &
  \begin{circuitikz}
    \draw (0,0) to [lamp] (2,0);
  \end{circuitikz}
\\ \hline 
  this one is centred vertically   &
  \begin{circuitikz}
    \draw (0,0) to [european resistor] (2,0);
  \end{circuitikz}
\\ \hline
  but this on is too high &
  \begin{circuitikz}
    \draw (0,0) to [empty diode,anchor=west] (2,0);
  \end{circuitikz}
\\ \hline
\end{tabular}%

\renewcommand{\arraystretch}{2.5}
\begin{tabular}{|c|c|}
  \hline
  too high &
  \begin{circuitikz}
    \draw (0,0) to [lamp] (2,0);
  \end{circuitikz}
\\ \hline 
  this one is centred vertically   &
  \begin{circuitikz}
    \draw (0,0) to [european resistor] (2,0);
  \end{circuitikz}
\\ \hline
  but this on is too high &
  \begin{circuitikz}
    \draw (0,0) to [empty diode,anchor=west] (2,0);
  \end{circuitikz}
\\ \hline
\end{tabular}
}

\end{document}

那么,您可能会说:“但是第二个表格中的图片没有居中!”。好吧,文本也没有居中!当您更改时\arraystretch,文本将不会居中,因此,基线不会改变,图片也不会移动。

\arraystretch但是,如果你真的坚持这样做,你可以在和使图片居中的位置之间建立线性关系。我建议反对但是使用这个:

在此处输入图片描述

但是互联网上的人们往往不同意某些事情,因此如果您使用此方案,则必须更改\arraystretch,然后使用\calcshift,然后使用\tikzset{baseline={([yshift=\yshift]current bounding box.center)}}。请注意,文本仍位于通常的位置...

\documentclass[11pt]{article}
\usepackage{circuitikz}

\newdimen\yshift
\def\calcshift{\yshift\dimexpr-0.6ex-0.5\dimexpr\arraystretch ex-1ex\relax\relax}

\begin{document}

{%
\calcshift
\tikzset{baseline={([yshift=\yshift]current bounding box.center)}}

\begin{tabular}{|c|c|}
  \hline
  too high &
  \begin{circuitikz}
    \draw (0,0) to [lamp] (2,0);
  \end{circuitikz}
\\ \hline 
  this one is centred vertically   &
  \begin{circuitikz}
    \draw (0,0) to [european resistor] (2,0);
  \end{circuitikz}
\\ \hline
  but this on is too high &
  \begin{circuitikz}
    \draw (0,0) to [empty diode,anchor=west] (2,0);
  \end{circuitikz}
\\ \hline
\end{tabular}

\renewcommand{\arraystretch}{2.5}
\calcshift
\tikzset{baseline={([yshift=\yshift]current bounding box.center)}}

\begin{tabular}{|c|c|}
  \hline
  too high &
  \begin{circuitikz}
    \draw (0,0) to [lamp] (2,0);
  \end{circuitikz}
\\ \hline 
  this one is centred vertically   &
  \begin{circuitikz}
    \draw (0,0) to [european resistor] (2,0);
  \end{circuitikz}
\\ \hline
  but this on is too high &
  \begin{circuitikz}
    \draw (0,0) to [empty diode,anchor=west] (2,0);
  \end{circuitikz}
\\ \hline
\end{tabular}

\renewcommand{\arraystretch}{5}
\calcshift
\tikzset{baseline={([yshift=\yshift]current bounding box.center)}}

\begin{tabular}{|c|c|}
  \hline
  too high &
  \begin{circuitikz}
    \draw (0,0) to [lamp] (2,0);
  \end{circuitikz}
\\ \hline 
  this one is centred vertically   &
  \begin{circuitikz}
    \draw (0,0) to [european resistor] (2,0);
  \end{circuitikz}
\\ \hline
  but this on is too high &
  \begin{circuitikz}
    \draw (0,0) to [empty diode,anchor=west] (2,0);
  \end{circuitikz}
\\ \hline
\end{tabular}
}

\end{document}

相关内容