使用半自定义样式对齐 .tikz pgfplots

使用半自定义样式对齐 .tikz pgfplots

以前,在这个问题中,有人帮助我解决了如何在带有可选子图的表格中对齐和定位图形:叠加来自输入的两个 .tikz 文件

它工作得很好,但我不喜欢使用缩放选项来缩小图表。我更喜欢只指定宽度和高度,让 latex 负责字体大小。它使文章更加一致。

因此,在创建图形时,我为其提供一种样式,允许在序言中批量设置宽度和高度,然后编辑下面与该图形相关的各个条目。

不幸的是,使用这个系统再次导致对齐失效。似乎只有当我使用定义序言中所有条目的自定义样式,或者当我根本不使用自定义样式并将 .tikz 图片中的宽度和高度设置为相同时,对齐才会保留。

完整的 MWE 显示我的问题:

\documentclass{article}
\usepackage{graphicx,array,booktabs,pgfplots}
\pgfplotsset{compat=1.9}
\usetikzlibrary{calc}

\pgfplotsset{inTable/.style={
width=4.0cm,
height=4.0cm,
}}

\newsavebox{\mybox}
\sbox{\mybox}{
\begin{tikzpicture}
\begin{axis}[inTable,
scale only axis,
xmin=1,
xmax=500,
xlabel={A},
ymin=0.5,
ymax=1,
ylabel={A}]

\addplot [color=blue,solid,line width=1.0pt,forget plot]
  table[row sep=crcr]{
10  0.7  \\
11  0.7  \\
12  0.7  \\
};
\end{axis}
\end{tikzpicture}}

\newsavebox{\myboxB}
\sbox{\myboxB}{
\begin{tikzpicture}
\begin{axis}[inTable,
scale only axis,
xmin=1,
xmax=200,
xlabel={B},
ymin=0,
ymax=3,
ylabel={B}]

\addplot [color=blue,solid,line width=1.0pt,forget plot]
  table[row sep=crcr]{
10  0.5  \\
11  0.6  \\
12  0.7  \\
};
\end{axis}
\end{tikzpicture}}

\begin{document}

\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\begin{table}[htb!]
 \centering
     \begin{tabular}{P{6.5cm} P{6.5cm}}
     \toprule
      Expt1 & Expt2\\
      \cmidrule(r){1-1}\cmidrule(l){2-2}
      \begin{tikzpicture}[baseline=(a.base)]
      \node[outer sep=0pt]  (a){\usebox{\mybox}};
       %%% Change dimensions in $(a.east)+(-0.9,0cm)$
%      \node[anchor=east,outer sep=0pt] at
%               ($(a.east)+(-0.9,0cm)$){\usebox{\mybox}};
      \end{tikzpicture}
      {\usebox{\mybox}}
      &
      {\usebox{\mybox}}
      {\usebox{\myboxB}}
      \\ \bottomrule
      \end{tabular}
      \caption{Test}
      \label{tbl:1}
\end{table}

\end{document}

错位

非常感谢您的帮助。这应该是我的表格-图形-插图绘图协议的最后一部分。

答案1

好的,谢谢 Torbjørn T,这个手册唤醒了我的记忆。

首先,因为左上角的图片实际上是在环境{tikzpicture}内部\begin{tikzpicture}。我必须对右上角的图片做同样的事情。然后[baseline, trim axis left, trim axis right]向每个图片添加对齐标志\begin{tikzpicture}(我从实际文档中的 .tikz 文件导入的标志,可以修复对齐问题。

我终于找到了一种可以完全协调和控制情节和子情节的方法。感谢所有帮助过我的人。

完整 MEW:

\documentclass{article}
\usepackage{graphicx,array,booktabs,pgfplots}
\pgfplotsset{compat=1.9}
\usetikzlibrary{calc}

\pgfplotsset{inTable/.style={
width=4.0cm,
height=4.0cm, %Change these numbers to ovveride all graphs in the table sizes in the table
}}

\newsavebox{\mybox}
\sbox{\mybox}{
\begin{tikzpicture}[baseline, trim axis left, trim axis right]
\begin{axis}[inTable,
scale only axis,
baseline,
xmin=1,
xmax=500,
xlabel={A},
ymin=0.5,
ymax=1,
ylabel={A}]

\addplot [color=blue,solid,line width=1.0pt,forget plot]
  table[row sep=crcr]{
10  0.7  \\
11  0.7  \\
12  0.7  \\
};
\end{axis}
\end{tikzpicture}}

\newsavebox{\myboxB}
\sbox{\myboxB}{
\begin{tikzpicture}[baseline, trim axis left, trim axis right]
\begin{axis}[inTable,
scale only axis,
xmin=1,
xmax=200,
xlabel={B},
ymin=0,
ymax=3,
ylabel={B}]

\addplot [color=blue,solid,line width=1.0pt,forget plot]
  table[row sep=crcr]{
10  0.5  \\
11  0.6  \\
12  0.7  \\
};
\end{axis}
\end{tikzpicture}}

\begin{document}

\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\begin{table}[htb!]
 \centering
     \begin{tabular}{P{6.5cm} P{6.5cm}}
     \toprule
      Expt1 & Expt2\\
      \cmidrule(r){1-1}\cmidrule(l){2-2}
      \begin{tikzpicture}[baseline=(a.base)]
      \node[outer sep=0pt]  (a){\usebox{\mybox}};
       %%% Change dimensions in $(a.east)+(-0.9,0cm)$
%      \node[anchor=east,outer sep=0pt] at
%               ($(a.east)+(-0.9,0cm)$){\usebox{\mybox}};
      \end{tikzpicture}
      {\usebox{\mybox}}
      &
      \begin{tikzpicture}[baseline=(a.base)]
      \node[outer sep=0pt]  (a){\usebox{\mybox}};
       %%% Change dimensions in $(a.east)+(-0.9,0cm)$
%      \node[anchor=east,outer sep=0pt] at
%               ($(a.east)+(-0.9,0cm)$){\usebox{\mybox}};
      \end{tikzpicture}
      {\usebox{\myboxB}}
      \\ \bottomrule
      \end{tabular}
      \caption{Test}
      \label{tbl:1}
\end{table}

\end{document}

相关内容