导致子图中 tikz 图错位的一般原因

导致子图中 tikz 图错位的一般原因

我经常会遇到一个 tikz 子图错位的情况。tikz 文件是用 Matlab 生成的,包含大量数据。如果我将所有子图替换为相同的示例图像、简单或相同的 tikz 文件,则不会发生这种情况。每次发生这种情况时,我通常没有时间浏览每个图的 500 多行 tikz 代码,所以我想知道是否存在已知的有关 tikz 图错位的一般问题可能导致此行为,以便我更容易识别问题。

我附上了一个结果示例,以及相应的子图代码。如您所见,右下角的图未对齐,左上角的标题也未对齐(略高)。

在此处输入图片描述

\begin{figure*}[ht]
\centering
\begin{subfigure}[b]{0.48\textwidth}
\centering
\scriptsize
\setlength\figureheight{4cm}
\setlength\figurewidth{6cm} 
\caption{knee Position}
\includetikz{balancingV1position}
%\includegraphics[width=1\textwidth]{example-image-a}
\label{fig::balancingV1position}
\end{subfigure}
~
\begin{subfigure}[b]{0.48\textwidth}
\centering
\scriptsize
\setlength\figureheight{4cm}
\setlength\figurewidth{6cm}
\caption{motor force}
\includetikz{balancingV1torque}
%\includegraphics[width=1\textwidth]{example-image-b}
\label{fig::balancingV1torque}
\end{subfigure}
~
\begin{subfigure}[b]{0.48\textwidth}
\centering
\scriptsize
\setlength\figureheight{4cm}
\setlength\figurewidth{6cm} 
\caption{nut velocity}
\includetikz{balancingV1velocity}
%\includegraphics[width=1\textwidth]{example-image-a}
\label{fig::balancingV1velocity}
\end{subfigure}
~
\begin{subfigure}[b]{0.48\textwidth}
\centering
\scriptsize
\setlength\figureheight{4cm}
\setlength\figurewidth{6cm}
\caption{\acs{com} angle}
\includetikz{balancingV1phi}
%\includegraphics[width=1\textwidth]{example-image-b}
\label{fig::balancingV1phi}
\end{subfigure}
\caption{text}
\label{fig::balancingV1}
\end{figure*}

% \includetikz is a new command that I've defined to load tikz files from a specific directory. It's not interesting.
% The purpose of this code is showing you the symmetry of the main code, I'm convinced that the problem occurs due to the tikz files, not here.

让我再次强调,我正在寻找普遍已知的原因(和解决方案),因为我偶尔会遇到这个问题,而且它似乎是随机的,这就是为什么我没有发布包含特定 tikz 文件的 MWE。

答案1

例如尝试以下代码:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=3cm,height=3cm}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=2,ymin=0,ymax=2,ytick=0.5,xtick=0.5]
\end{axis}
\draw [red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=2,ymin=0,ymax=2,ytick=2,xtick=0.5]
\end{axis}
\draw [red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=2,ymin=0,ymax=2,ytick=1,xtick=2]
\end{axis}
\draw [red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=2,ymin=0,ymax=2,ytick=2,xtick=2]
\end{axis}
\draw [red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}
\end{document}

生成以下输出:

在此处输入图片描述

如您所见,在轴边缘添加刻度会修改边界框,不同宽度的刻度(和轴标签)也会修改边界框,参见左侧的两个轴。我认为这就是为什么左上角和右下角图中的标题离轴较远的原因。

相关内容