![如何使两个不同的环境彼此相邻?](https://linux22.com/image/432729/%E5%A6%82%E4%BD%95%E4%BD%BF%E4%B8%A4%E4%B8%AA%E4%B8%8D%E5%90%8C%E7%9A%84%E7%8E%AF%E5%A2%83%E5%BD%BC%E6%AD%A4%E7%9B%B8%E9%82%BB%EF%BC%9F.png)
我在列表形式中写了一段代码。然后我想把它的运行结果图放在这段代码下面。但是在Latex中,虽然我把它们写在一起,但是有时候它们却分布在不同的页面上。注意有单独的标题。我看了其他帖子,我看了其他帖子,它们都是使用同一个环境并排的。比如两张图片并排,就可以在figure环境中使用minipage。但是如果我这样使用,我写的代码的标题也会以Figure开头。我该怎么办?谢谢!
在下面的代码中,它们彼此相邻。但有时它们不是。我希望能够将它们彼此相邻地设置。
\documentclass{article}
\usepackage{listings}
%\usepackage{caption,subcaption}
\usepackage{subfig}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{algorithmic}
\usepackage[ruled,lined]{algorithm2e}
\usepackage{lipsum}
\usepackage{mathtools}
%\usepackage{subfigure}
\usepackage{subfig}
\usepackage{enumitem}
\usepackage{pifont}
\newlength{\restofline}
\usepackage{multirow}
\usepackage{tablefootnote}
\usepackage{listings}
\usepackage{framed}
\usepackage{capt-of}
\usepackage{newfloat}
\usepackage{xcolor}
\definecolor{keywordcolor}{RGB}{157,0,129}
\definecolor{commentcolor}{RGB}{157,0,129}
\definecolor{numbercolor}{RGB}{120,120,120}
\DeclareFloatingEnvironment[fileext=frm,placement={!ht},name=Listing]{listing}
\begin{document}
\lipsum[1-2]
\begin{listing}[t]
\noindent
\centering
\begin{minipage}[t]{0.45\textwidth}
\begin{lstlisting}[gobble=16, language=C,
keywordstyle={\color{keywordcolor}},basicstyle = \sffamily,
xleftmargin=2em,xrightmargin=2em,frame=lrtb,numberstyle={\color{numbercolor}\normalfont}, numbersep={-0.2cm},numbers=left,tabsize=7,
basicstyle=\linespread{1.1}\footnotesize]
int main(){
printf("hello")
}
\end{lstlisting}
\end{minipage}%
\caption{Code.}
\end{listing}
\begin{figure}[t]
\centering
\begin{minipage}{0.5\textwidth}
\centering
% \includegraphics[width=0.9\linewidth]{hpca/latency_cdf.pdf}
a picture
\caption{The latencies in accessing the replacement set when the target set contains a different number of dirty cache lines. }
\label{CDF}
\end{minipage}\hfill
\end{figure}
\lipsum
\end{document}
答案1
只需使用两个长度为 的小页面,4.5\textwidth
即可在第一个小页面中获取列表,并在第二个小页面中获取图像。
请参阅以下 MWE(重要的代码更改以 标记<======
):
\documentclass{article}
\usepackage{listings}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{algorithmic}
\usepackage[ruled,lined]{algorithm2e}
\usepackage{lipsum}
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage{pifont}
\newlength{\restofline}
\usepackage{multirow}
\usepackage{tablefootnote}
\usepackage{framed}
\usepackage{newfloat}
\usepackage{xcolor}
\definecolor{keywordcolor}{RGB}{157,0,129}
\definecolor{commentcolor}{RGB}{157,0,129}
\definecolor{numbercolor}{RGB}{120,120,120}
\DeclareFloatingEnvironment[fileext=frm,placement={!ht},name=Listing]{listing}
\begin{document}
\lipsum[1-2]
\begin{minipage}[c]{0.45\textwidth} % <=================================
\noindent
\centering
\begin{lstlisting}[gobble=16, language=C,
keywordstyle={\color{keywordcolor}},basicstyle = \sffamily,
xleftmargin=2em,xrightmargin=2em,frame=lrtb,numberstyle={\color{numbercolor}\normalfont},
numbersep={-0.2cm},numbers=left,tabsize=7,
basicstyle=\linespread{1.1}\footnotesize]
int main(){
printf("hello")
}
\end{lstlisting}
\captionof{listing}{Code.}
\end{minipage}% <=======================================================
\begin{minipage}[c]{0.45\textwidth} % <=================================
\centering
\includegraphics[width=0.9\linewidth]{example-image} % <==============
\captionof{figure}{The latencies in accessing the replacement set when the target set contains a different number of dirty cache lines. }
\label{CDF}
\end{minipage} % <======================================================
\lipsum
\end{document}
及其结果: