我想对齐两个在外部包含图例的地块:
\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{tikzscale}
\usepackage{subcaption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\begin{figure}
\begin{centering}
\begin{subfigure}[t]{0.4\textwidth}
\vskip 0pt
\includegraphics[width=\textwidth]{plot1.tikz}
\end{subfigure}
~
\begin{subfigure}[t]{0.4\textwidth}
\vskip 0pt
\includegraphics[width=\textwidth]{plot2.tikz}
\end{subfigure}
\end{centering}
\end{figure}
\end{document}
具体来说,这两个绘图文件包含以下内容:
% plot1.tikz
\begin{tikzpicture}
\begin{axis}[
legend entries={Short caption},
legend style = {at = {(0.5, -0.3)}, anchor = north},
width=\textwidth,
height=\textwidth]
\addplot[color=red,mark=x] coordinates {
(2,-2.8559703)
(8,-7.9377747) };
\end{axis}
\end{tikzpicture}
% plot2.tikz
\begin{tikzpicture}
\begin{axis}[
legend entries={Long long long long long long caption},
legend style = {at = {(0.5, -0.3)}, anchor = north},
width=\textwidth,
height=\textwidth]
\addplot[color=red,mark=x] coordinates {
(2,-2.8559703)
(8,-7.9377747) };
\end{axis}
\end{tikzpicture}
不幸的是,第二幅图的标题宽度超过了图本身的宽度。因此,图缩小了,尺寸不再匹配:
我希望图例的布局打破这条长线并保持固定的宽度。我该如何实现这个效果?
有办法吗?
答案1
您可以使用 指定换行符\\
,但此外您还必须修改legend style
并指定位置align
,例如cells={align=left}
。
另外,请注意,这\centering
是一个切换命令,而不是写成\begin{centering}...\end{centering}
。我帮你解决了这个问题。
\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{tikzscale}
\usepackage{subcaption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepackage{filecontents} % <--- To make a self contained MWE
\begin{filecontents*}{plot1.tikz}
\begin{tikzpicture}
\begin{axis}[
legend entries={Short caption},
legend style = {at = {(0.5, -0.3)}, anchor = north},
width=\textwidth,
height=\textwidth]
\addplot[color=red,mark=x] coordinates {
(2,-2.8559703)
(8,-7.9377747) };
\end{axis}
\end{tikzpicture}
\end{filecontents*}
\begin{filecontents*}{plot2.tikz}
\begin{tikzpicture}
\begin{axis}[
legend entries={Long long long \\ long long long \\ caption},
legend style = {at = {(0.5, -0.3)}, anchor = north,cells={align=left}},
width=\textwidth,
height=\textwidth]
\addplot[color=red,mark=x] coordinates {
(2,-2.8559703)
(8,-7.9377747) };
\end{axis}
\end{tikzpicture}
\end{filecontents*}
\begin{document}
\begin{figure}
\centering % <--- Fixed
\begin{subfigure}[t]{0.4\textwidth}
\vskip 0pt
\includegraphics[width=\textwidth]{plot1.tikz}
\end{subfigure}
~
\begin{subfigure}[t]{0.4\textwidth}
\vskip 0pt
\includegraphics[width=\textwidth]{plot2.tikz}
\end{subfigure}
\end{figure}
\end{document}