gpt4 book ai didi

latex - 如何在 latex 中对齐两个 tikzpicture 图

转载 作者:行者123 更新时间:2023-12-04 10:32:07 25 4
gpt4 key购买 nike

我正在尝试使用 https://www.latex-tutorial.com/tutorials/figures/ 中显示的子图方法制作并排图,但我似乎无法调整大小并使它们并排......我做错了什么?
下面是我正在使用的代码

\begin{figure}
\centering
\begin{subfigure}[b!]{0.3\textwidth}
\begin{tikzpicture}
\begin{axis}[
axis y line = middle,
axis x line = middle,
xlabel = $x$,
ylabel = {$f(x) = x^3$},
grid=major,
]
\addplot [
domain=-3:3,
samples=100,
color=red,
]
{x^3};
\addlegendentry{$x^3$}
%
\addplot [
domain=-3:3,
samples=100,
color=blue,
]
{x^3 + 3};
\addlegendentry{$x^3 + 3$}
%
\addplot [
domain=-3:3,
samples=100,
color=green,
]
{x^3 - 3};
\addlegendentry{$x^3 - 3$}
\end{axis}
\end{tikzpicture}
\end{subfigure}
%\hfill
\begin{subfigure}[b]{0.3\textwidth}
\begin{tikzpicture}
\begin{axis}[
axis y line = middle,
axis x line = middle,
xlabel = $x$,
ylabel = {$f(x) = x^3$},
grid=major,
]
\addplot [
domain=-3:3,
samples=100,
color=red,
]
{x^3};
\addlegendentry{$x^3$}
\end{axis}
\end{tikzpicture}
\end{subfigure}
\caption{lajsdfls}
\end{figure}

最佳答案

您的代码有两个问题。

数字的第一次水平对齐不正确,但这可以通过使用轻松修复

\begin{subfigure}[b]{0.3\textwidth}

代替
\begin{subfigure}[b!]{0.3\textwidth}

关于宽度,创建子图环境时所做的是创建指定宽度的小型页面。但是取决于您的内容是否尊重此宽度,不会进行重新缩放。

例如,如果在子图中包含一个图像并为其指定宽度\linewidth,则该宽度将被考虑。但是如果你给这张图片一个 15cm 的宽度,它可能会比你的 minipage 大。但是 LaTeX 会尊重你的指令(并表示一个过满的 hbox)。

这就是你遇到的问题。您的地块太大且重叠。

有两种方法可以解决这个问题。
  • 您可以为轴环境提供 width=\linewidth 参数,但通常需要重新设计绘图
  • 您可以重新缩放由 tikz 创建的框。最灵活的方法是使用 adjustbox 包
  • \documentclass{article}

    \usepackage{subcaption}
    \usepackage{tikz}
    \usepackage{pgfplots}
    \usepackage{adjustbox}

    \begin{document}

    \begin{figure}
    \centering
    \begin{subfigure}[b]{0.45\textwidth}
    %%% \begin{adjustbox}{width=\linewidth} % rescale box
    \begin{tikzpicture}
    \begin{axis}[
    %%% width=\linewidth, % or modify the plot width
    axis y line = middle,
    ...
    ...
    \end{axis}
    \end{tikzpicture}
    %%% \end{adjustbox} %
    \end{subfigure}%
    \hfill
    \begin{subfigure}[b]{0.45\textwidth}
    etc.

    向轴环境添加宽度参数

    enter image description here

    使用调整框重新缩放

    enter image description here

    顺便说一句,如果您不打算在绘图中添加子标题,则 subfigure 环境是无用的,您可以将(适当缩放的)tikzpictures 并排放置,并以\hfill 分隔。

    关于latex - 如何在 latex 中对齐两个 tikzpicture 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60366804/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com