如何在表格中对齐 tikzpicture 和文本?

如何在表格中对齐 tikzpicture 和文本?

我需要将 tikzpicture 和文本放在相同的高度。但是,我无法做到这一点。这是我的代码:

\begin{table}
\centering
\begin{tabularx}{\textwidth}{ |l|X| }
\hline
 \multicolumn{1}{c|}{
 \begin{tabular}[c]{@{}c@{}}   
  \begin{tikzpicture}
    \draw[red,pattern=north east lines, pattern color=red] (0,0.1) rectangle (1,0.4);
    \draw [blue,very thick](0,0) rectangle (4,0.5);
    \draw[thick,<->] (0,-0.2) -- (4,-0.2);
    \node at (2,-0.5) {$w/2$};
    \draw [blue,very thick](1,1.5) arc [radius=1, start angle=0, delta angle=-90];
    \draw[thick,<->] (0,1.5) -- (1,1.3);
    \node at (0.5,1.8) {$R$};
    \draw[very thick,->, red] (0,1.5) -- (0,0.9);
    \node[red] at (-0.2,1.2) {$\delta$};
    \draw[thick,<->] (4.2,0) -- (4.2,0.5);
    \node at (4.5,0.25) {$h$};
    \draw[thick,dashed,->](0,0) -- (4.7,0) node[anchor=north west] {x};
    \draw[thick,dashed,->] (0,0) -- (0,2.2) node[anchor=south east] {y};
   \end{tikzpicture} \\$h=1$\;\;$R=4.366h$\;\;$w/2=20h$
   \end{tabular}}
& very very long text........
   \end{tabularx}
   \end{table}

表格显示如下:在此处输入图片描述 我想要的是将文本放置在与图片相同的高度,我不想顶部留空。有人能帮助我吗?非常感谢!

答案1

此解决方案不需要任何额外的软件包。相反,它修改了可选参数,tabular具体方法如下大卫·卡莱尔的评论baseline,并在设置时使用键tikzpicture。 的使用baseline改编自 TiKZ 手册,大约第 125 页。

在考虑回答问题之前,需要进行以下修正以构建可用的 MWE:

  • 完成代码\documentclass等以创建起始示例;
  • 添加tabularxtikz根据需要包,只是kantlipsum为了方便长文本;
  • 添加所需的 TiKZ 库,patterns因为 LaTeX 抱怨如果没有它就无法编译;
  • 通过将数学命令放入数学环境中来纠正代码,使其能够编译(\:需要里面 $...$- 不在外面)。

回答问题时所做的更改:

  • \begin{tabular}[c]...说将表格垂直居中在当前基线上,但这并不是你想要的......

    • \begin{tabular}[t]...将顶线与当前基线对齐,我认为这就是您想要的......
  • tikzpicture具有默认对齐方式设置,以便图片的底部与当前基线对齐,并与图片相对于右侧第一行文本居中c的对齐方式相结合......这不是您想要的......tabular

    • baseline可用于调整对齐方式,tikzpicture例如我使用了一个命名节点,以便该节点的基线与当前基线对齐,并将其与选项相结合ttabular这会将该节点与右侧的第一行文本对齐...这可能更接近您想要的......

    • y是命名节点,其名称附加在y轴上的标签上,该节点似乎是图中最顶层的节点。

顶部与 y 节点对齐

\documentclass{article}
\usepackage{kantlipsum, tabularx, tikz}
\usetikzlibrary{patterns}
\begin{document}
  \begin{table}
    \centering
    \begin{tabularx}{\textwidth}{ |l|X| }
      \hline
      \multicolumn{1}{|c|}{
        \begin{tabular}[t]{@{}c@{}}
          \begin{tikzpicture}[baseline=(y)]
            \draw[red,pattern=north east lines, pattern color=red] (0,0.1) rectangle (1,0.4);
            \draw [blue,very thick](0,0) rectangle (4,0.5);
            \draw[thick,<->] (0,-0.2) -- (4,-0.2);
            \node at (2,-0.5) {$w/2$};
            \draw [blue,very thick](1,1.5) arc [radius=1, start angle=0, delta angle=-90];
            \draw[thick,<->] (0,1.5) -- (1,1.3);
            \node at (0.5,1.8) {$R$};
            \draw[very thick,->, red] (0,1.5) -- (0,0.9);
            \node[red] at (-0.2,1.2) {$\delta$};
            \draw[thick,<->] (4.2,0) -- (4.2,0.5);
            \node at (4.5,0.25) {$h$};
            \draw[thick,dashed,->](0,0) -- (4.7,0) node[anchor=north west] {x};
            \draw[thick,dashed,->] (0,0) -- (0,2.2) node (y) [anchor=south east] {y};
          \end{tikzpicture} \\$h=1\;\;R=4.366h\;\;w/2=20h$
        \end{tabular}}
      & \kant[1]\\\hline
    \end{tabularx}
  \end{table}
\end{document}

实现相同结果的另一种方法是使用

  • \begin{tabular}[t]...
  • \begin{tikzpicuter}[baseline=(current bounding box.north)

如果您希望列垂直居中,则可以\parbox对右列中的文本使用垂直居中。

  \begin{table}
    \centering
    \begin{tabularx}{\textwidth}{ |l|X| }
      \hline
      \multicolumn{1}{|c|}{
        \begin{tabular}[c]{@{}c@{}}
          \begin{tikzpicture}[baseline=(current bounding box.center)]
            \draw[red,pattern=north east lines, pattern color=red] (0,0.1) rectangle (1,0.4);
            \draw [blue,very thick](0,0) rectangle (4,0.5);
            \draw[thick,<->] (0,-0.2) -- (4,-0.2);
            \node at (2,-0.5) {$w/2$};
            \draw [blue,very thick](1,1.5) arc [radius=1, start angle=0, delta angle=-90];
            \draw[thick,<->] (0,1.5) -- (1,1.3);
            \node at (0.5,1.8) {$R$};
            \draw[very thick,->, red] (0,1.5) -- (0,0.9);
            \node[red] at (-0.2,1.2) {$\delta$};
            \draw[thick,<->] (4.2,0) -- (4.2,0.5);
            \node at (4.5,0.25) {$h$};
            \draw[thick,dashed,->](0,0) -- (4.7,0) node[anchor=north west] {x};
            \draw[thick,dashed,->] (0,0) -- (0,2.2) node [anchor=south east] {y};
          \end{tikzpicture} \\$h=1\;\;R=4.366h\;\;w/2=20h$
        \end{tabular}}
      & \parbox[c]{\linewidth}{\kant[1]}\\\hline
    \end{tabularx}
  \end{table}

中心柱

不过,我还是强烈建议您看一下booktabs专为排版专业品质表格而设计的软件包。特别是,它增强了间距并提供了更灵活的规则。其中一项建议是完全放弃垂直规则,并使用稍粗的水平规则来开始和结束表格。

使用 <code>booktabs</code> 制作表格

\documentclass{article}
\usepackage{kantlipsum, tabularx, tikz, booktabs}
\usetikzlibrary{patterns}
\begin{document}
  \begin{table}
    \centering
    \begin{tabularx}{\textwidth}{ lX }
      \toprule
      \multicolumn{1}{c}{
        \begin{tabular}[c]{@{}c@{}}
          \begin{tikzpicture}[baseline=(current bounding box.center)]
            \draw[red,pattern=north east lines, pattern color=red] (0,0.1) rectangle (1,0.4);
            \draw [blue,very thick](0,0) rectangle (4,0.5);
            \draw[thick,<->] (0,-0.2) -- (4,-0.2);
            \node at (2,-0.5) {$w/2$};
            \draw [blue,very thick](1,1.5) arc [radius=1, start angle=0, delta angle=-90];
            \draw[thick,<->] (0,1.5) -- (1,1.3);
            \node at (0.5,1.8) {$R$};
            \draw[very thick,->, red] (0,1.5) -- (0,0.9);
            \node[red] at (-0.2,1.2) {$\delta$};
            \draw[thick,<->] (4.2,0) -- (4.2,0.5);
            \node at (4.5,0.25) {$h$};
            \draw[thick,dashed,->](0,0) -- (4.7,0) node[anchor=north west] {x};
            \draw[thick,dashed,->] (0,0) -- (0,2.2) node [anchor=south east] {y};
          \end{tikzpicture} \\$h=1\;\;R=4.366h\;\;w/2=20h$
        \end{tabular}}
      & \parbox[c]{\linewidth}{\kant[1]}\\\bottomrule
    \end{tabularx}
  \end{table}
\end{document}

答案2

您可能需要一个adjustbox具有顶部对齐的环境tabular

因此,加载adjustbox打包并tabular嵌入

\begin{adjustbox}{valign=t}
....
\end{adjustbox}

梅威瑟:

\documentclass{article}
\usepackage{tabularx}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{lipsum} % just for the example
\usepackage{adjustbox}


\begin{document}
\begin{table}
\centering
\begin{tabularx}{\textwidth}{ |l|X| }
\hline
 \multicolumn{1}{c|}{%
 \begin{adjustbox}{valign=t}
 \begin{tabular}[c]{@{}c@{}}
  \begin{tikzpicture}
    \draw[red,pattern=north east lines, pattern color=red] (0,0.1) rectangle (1,0.4);
    \draw [blue,very thick](0,0) rectangle (4,0.5);
    \draw[thick,<->] (0,-0.2) -- (4,-0.2);
    \node at (2,-0.5) {$w/2$};
    \draw [blue,very thick](1,1.5) arc [radius=1, start angle=0, delta angle=-90];
    \draw[thick,<->] (0,1.5) -- (1,1.3);
    \node at (0.5,1.8) {$R$};
    \draw[very thick,->, red] (0,1.5) -- (0,0.9);
    \node[red] at (-0.2,1.2) {$\delta$};
    \draw[thick,<->] (4.2,0) -- (4.2,0.5);
    \node at (4.5,0.25) {$h$};
    \draw[thick,dashed,->](0,0) -- (4.7,0) node[anchor=north west] {x};
    \draw[thick,dashed,->] (0,0) -- (0,2.2) node[anchor=south east] {y};
   \end{tikzpicture} \\$h=1\;\;R=4.366h\;\;w/2=20h$
 \end{tabular}
 \end{adjustbox}}
& \lipsum[2]\\\hline
   \end{tabularx}
   \end{table}
\end{document} 

输出:

在此处输入图片描述

相关内容