两个数字的组合留有右边距

两个数字的组合留有右边距

我的作品中的图像留有右边距,如何限制它们移出? 两幅图像是 fig_4 和 fig_5 ;两者都是 .tikz 文件,与 .main 文件位于不同的文件夹中。

代码:

\begin{figure}[t]
        \centering
        \begin{minipage}{0.45\textwidth}
            \centering
            \input{figs/fig_4} % first figure itself
            \caption{Loss plots for the configuration 3Layers and 5 Neurons in each layer}
            \label{fig_l1}
        \end{minipage}\hfill
        \begin{minipage}{0.45\textwidth}
            \centering
             \input{figs/fig_5} % second figure itself
            \caption{Loss plots for the configuration 5Layers and 3 Neurons in each layer}
            \label{fig_l2}
        \end{minipage}
\end{figure}

代码(图5)右侧:

\begin{tikzpicture}
\begin{semilogyaxis}[
     xmin=0, xmax=1e+4,      % The output would not be visible 
     ymin=1e-4, ymax=1,  % The output would not be visible 
    xlabel={Number of Epochs}, 
    ylabel={Normalized MSE: $\log_{10} (e)$},
    label style={font=\bfseries\boldmath},
    tick label style={font=\bfseries\boldmath},
]
\addplot[scatter, no marks, draw=blue] table [x=Epochs, y=Test_loss, col sep=comma] {5N_3H.csv};
\addplot[scatter, no marks, draw=red] table [x=Epochs, y=Train_loss, col sep=comma] {5N_3H.csv};
\end{semilogyaxis}
\end{tikzpicture}

在此处输入图片描述

答案1

不幸的是,您没有提供完整的 MWE(例如,我们缺少您的 *.csv 文件、您的 documentclass 和您的(相关)包),所以我必须使用一些示例数据,但我尝试尽可能少地更改您的代码。

糟糕的选择

您可以将图形包装成\resizebox类似\resizebox{\linewidth}{!}{\input{figs/fig_5}}。这会将整个图(包括刻度、刻度标签、轴标签、线宽等)缩放到\linewidth(或您在第一个参数中输入的任何值)。很酷的事情是,您可以精确使用您拥有的空间,缺点是您得到不一致的图形和字体,因为它们与其余部分一起缩放。

好的选择

缩放图,但其他一切保持原样:您可以添加scale=<value>到轴,这将完成此操作。唯一的缺点是您必须<value>手动选择。

最佳选择

如果您使用“good”选项,则必须fig_5手动修改文件,如果文件是自动生成的(例如通过 Matlab2Tikz 或类似工具),这不是一个好主意。因此,您可以\pgfplotsset{every axis/.append style={scale=<value>}}在输入文件之前使用。由于它位于图形内部,因此此更改将是局部更改。

包含所有选项的代码
\begin{filecontents*}{fig5.tex}
    \begin{tikzpicture}
        \begin{semilogyaxis}[
            xmin=0, xmax=1e+4, % The output would not be visible 
            ymin=1e-4, ymax=1, % The output would not be visible 
            xlabel={Number of Epochs}, 
            ylabel={Normalized MSE: $\log_{10} (e)$},
            label style={font=\bfseries\boldmath},
            tick label style={font=\bfseries\boldmath},
            % scale=0.8, % GOOD option
        ]
            \addplot[scatter, no marks, draw=blue] table [row sep=crcr] { 0  1 \\ 10000  0.0001\\};
            \addplot[scatter, no marks, draw=red ] table [row sep=crcr] { 0  1 \\ 10000  0.0001\\};
        \end{semilogyaxis}
    \end{tikzpicture}
\end{filecontents*}

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
    \begin{figure}[t]
        \centering
        \begin{minipage}{0.45\textwidth}
            \centering
            %\resizebox{\linewidth}{!}{\input{fig5}} % BAD option
            \pgfplotsset{every axis/.append style={scale=0.8}}
            \input{fig5} % first figure itself
            \caption{Loss plots for the configuration 3 Layers and 5 Neurons in each layer}
            \label{fig_l1}
        \end{minipage}\hfill
        \begin{minipage}{0.45\textwidth}
            \centering
            %\resizebox{\linewidth}{!}{\input{fig5}} % BAD option
            \pgfplotsset{every axis/.append style={scale=0.8}} % BEST option
            \input{fig5} % second figure itself
            \caption{Loss plots for the configuration 5 Layers and 3 Neurons in each layer}
            \label{fig_l2}
        \end{minipage}
\end{figure}
\end{document}
漂亮的截图

结果

答案2

如果你要将 tikzpictures 放入单独的文件中,为什么不使用独立文件?更容易调试且使用速度更快。

棘手的是,您需要\textwidth在两个文件中使用相同的内容。一种方法是使用 standalone 的 [class=...] 选项。B 计划是\the\textwidth在 standalone 中编写并手动设置它。

\documentclass{article}
\usepackage{graphicx}
\usepackage{showframe}% or [showframe] for geometry
\begin{document}

%\the\textwidth% plan B

\begin{figure}[t]
        \centering
        \begin{minipage}{0.45\textwidth}
            \centering
            \includegraphics{test5}% or fig/fig5 if set up that way
            \caption{Loss plots for the configuration 3Layers and 5 Neurons in each layer}
            \label{fig_l1}
        \end{minipage}\hfill
        \begin{minipage}{0.45\textwidth}
            \centering
            \includegraphics{test5}% or fig/fig5 if set up that way
            \caption{Loss plots for the configuration 5Layers and 3 Neurons in each layer}
            \label{fig_l2}
        \end{minipage}
\end{figure}

\end{document}

如果没有 csv 文件和较窄的宽度,生成的 tikzpicture 看起来会很糟糕,但这是一个美学问题。

\documentclass[class=article]{standalone}
\usepackage{pgfplots}

%\setlength{\textwidth}{345pt}% plan B

\begin{document}
\begin{tikzpicture}
\begin{semilogyaxis}[
     xmin=0, xmax=1e+4,      % The output would not be visible 
     ymin=1e-4, ymax=1,  % The output would not be visible 
    xlabel={Number of Epochs}, 
    ylabel={Normalized MSE: $\log_{10} (e)$},
    label style={font=\bfseries\boldmath},
    tick label style={font=\bfseries\boldmath},
    width=0.45\textwidth
]
\addplot[scatter, no marks, draw=blue] table [x=Epochs, y=Test_loss, col sep=comma] {5N_3H.csv};
\addplot[scatter, no marks, draw=red] table [x=Epochs, y=Train_loss, col sep=comma] {5N_3H.csv};
\end{semilogyaxis}
\end{tikzpicture}
\end{document}

相关内容