我是新手。我一直在尝试学习如何将这些图并排格式化在一行上,但一直无法成功。我不明白如何使用 \begin{figure}[h] 功能或子图。任何帮助都将不胜感激。
**\documentclass[a4paper,12pt,twoside]{book}
\usepackage{amsmath,bm, graphicx, pgfplots}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepackage{graphicx}
\graphicspath{ {Figure_1.png}, {logo.gif} }
\usepackage{amsmath}
\usepackage{nccmath}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{hyphenat}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{subfig}
\usepackage{multirow}
\usepackage{array}
\usepackage{graphicx,subfigure}
\begin{document}
\begin{figure}[h]
\centering
\subfigure[Figure A]{\label{fig:a}
\begin{tikzpicture}[baseline=0pt]
\begin{axis}[
xmin=-2.5, xmax=2.5,
ymin=-1.5, ymax=1.5,
axis lines=center,
axis on top=true,
domain=-2.5:2.5,
ylabel=$y$,
xlabel=$x$,
]
\\
\addplot [mark=none,draw=red,ultra thick] {0.5*(1 +tanh(\x))};
\node [right, red] at (axis cs: -1.1,1.1) {$y = 0.5 *(1 + \tanh x)$};
%% Add the asymptotes
\draw [blue, dotted, thick] (axis cs:-2.5,0)-- (axis cs:2.5,0);
\draw [blue, dotted, thick] (axis cs:-2.5,+1)-- (axis cs:2.5,+1);
\end{axis}
\end{tikzpicture}
}
\subfigure[Figure B]{\label{fig:b}
\begin{tikzpicture}[
declare function={
func(\x)= (\x <= 0) * (0) +
and(\x > 0 , \x < 1) * (\x) +
(\x >= 1) * (1)
;
}
]
\begin{axis}[
xmin=-2.5, xmax=2.5,
ymin=-1.5, ymax=1.5,
axis lines=center,
axis on top=true,
domain=-2.5:2.5,
ylabel=$y$,
xlabel=$x$,
]
\addplot [blue,thick] {func(\x)};
\end{axis}
\end{tikzpicture}
}
\subfigure[Figure B]{\label{fig:c}
\begin{tikzpicture}[
declare function={
gunc(\x)= (\x <= 0) * (0) +
(\x > 0) * (\x)
;
}
]
\begin{axis}[
xmin=-2.5, xmax=2.5,
ymin=-1.5, ymax=1.5,
axis lines=center,
axis on top=true,
domain=-2.5:2.5,
ylabel=$y$,
xlabel=$x$,
]
\addplot [cyan,thick] {gunc(\x)};
\end{axis}
\end{tikzpicture}
\label{fig:image3}
}
\end{figure}
\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}
\end{document}**
答案1
您的文档示例存在许多问题:
每个包仅加载一次(例如,您加载
graphicx
三次!)。请注意,如果任何已加载的包已过时,例如
subfigure
被替换subfig
,但是对于子图和表来说最强大的包是subcaption
包。对于浮动位置,您应该启用该功能,以便 LaTeX 可以选择文档中的最佳位置。例如,仅使用
h
不启用该功能,因此最好添加选项t
(顶部) 或b
(底部) 或p
(页面) 或所有选项的组合。许多人,他们不会对浮动位置感到“无聊”,在序言中添加
\makeatletter \def\fps@figure{hbtp} \def\fps@table{hbtp} \makeatother
然后不写浮动位置(参见下面的 MWE)。
如果可能的话,使用最新版本的软件包(它们具有新的、改进的功能,删除了发现的错误等。例如,
pgfplots
使用最新版本 1.18 而不是老版本 1.7。熟悉使用过的软件包:阅读它们的文档(有些软件包有大量的文档,但这些文档通常包含使用教程)
因此,对于您的文档,我将使用最新版本的 LaTeX 和所有软件包的序言,如以下 MWE 所示(基于我的答案关于你之前的一个问题:
\documentclass[a4paper,12pt,twoside]{book}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath,bm}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{graphicx}
\graphicspath{ {Figure_1.png}, {logo.gif} }
\usepackage{nccmath} % it load amsmath too
\usepackage{array, multirow}
\usepackage{caption}
\usepackage{subcaption}
\makeatletter
\def\fps@figure{hbtp}
\def\fps@table{hbtp}
\makeatother
\usepackage{hyphenat}
\begin{document}
\begin{figure}
\centering
\pgfplotsset{
xmin=-2.5, xmax=2.5,
ymin=-1.5, ymax=1.5,
axis lines=center,
axis on top=true,
domain=-2.5:2.5,
ylabel=$y$,
xlabel=$x$,
}
\begin{subfigure}[b]{0.3\textwidth}
\begin{tikzpicture}[scale=0.6,]
\begin{axis}
\addplot [mark=none,draw=red,ultra thick] {0.5*(1 +tanh(\x))};
\node [right, red] at (-1.1,1.1) {$y = 0.5 *(1 + \tanh x)$};
%% Add the asymptotes
\draw [blue, dotted, thick] (-2.5,0) -- (2.5,0);
\draw [blue, dotted, thick] (-2.5,1) -- (2.5,1);
\end{axis}
\end{tikzpicture}
\caption{one}
\label{fig:a}
\end{subfigure}%
\hfill%
\begin{subfigure}[b]{0.3\textwidth}
\begin{tikzpicture}[scale=0.6,
declare function={func(\x)= (\x <=0) * (0) +
and(\x >0, \x < 1) * (\x) +
(\x >=1) * (1);}
]
\begin{axis}
\addplot [blue,thick] {func(\x)};
\end{axis}
\end{tikzpicture}
\caption{two}
\label{fig:b}
\end{subfigure}%
\hfill%
\begin{subfigure}[b]{0.3\textwidth}
\begin{tikzpicture}[scale=0.6,
declare function={gunc(\x)= (\x <=0) * (0) +
(\x > 0) * (\x);}
]
\begin{axis}
\addplot [cyan,thick] {gunc(\x)};
\end{axis}
\end{tikzpicture}
\caption{three}
\label{fig:c}
\end{subfigure}
\caption{Three graphs}
\label{fig:image3}
\end{figure}
\end{document}