如何将列表块和图像并排放置?

如何将列表块和图像并排放置?

为了进行微调,我必须将代码列表和结果作为图像并排显示,否则它将显示在下一页上,我必须对其进行极端裁剪。

以下是我目前的进展:

\begin{lstlisting}[captionpos=b,caption={Alle HTML Überschriften (Quellcode)},label=lst:headings]
<h1>Überschrift erster Ordnung</h1>
<h2>Überschrift zweiter Ordnung</h2>
<h3>Überschrift dritter Ordnung</h3>
<h4>Überschrift vierter Ordnung</h4>
<h5>Überschrift fünfter Ordnung</h5>
<h6>Überschrift sechster Ordnung</h6>
\end{lstlisting}

\begin{figure}[h]
\includegraphics[height = 100px]{headings1}
\caption{Alle HTML Überschriften (Ansicht) \label{headingsimage}}
\end{figure}

我怎样才能将它们放在一起?

答案1

我建议使用 的解决方案\minipage,如果几行太长并且会被换行,代码可能会看起来很丑陋,所以你必须选择好的minipage宽度参数(考虑两个块之间的空间;不要使用 100% 的线宽)、图像缩放scalelinewidth获得漂亮的形状。我希望我回答了你的问题。

\documentclass{report}
\usepackage{float}
\usepackage{listings}
\usepackage{graphicx}

\begin{document}
\begin{figure}[h]
\begin{minipage}{.5\textwidth}  %listing bloc will have 50% of the line width 
\lstset{linewidth = 4cm, breaklines=true} %set your listing lines widths, and set breaklines to true
\begin{lstlisting}
Your code here here..........
Your code here here..........
Your code here here..........
\end{lstlisting}
\end{minipage}
\qquad %space between listing bloc and the figure
\begin{minipage}{0.4\textwidth} %figure will have the remaning 40% of the line width
\includegraphics[scale=.4]{example-image}  %the image must be resized or scaled if needed
\caption{caption}
\end{minipage}
\end{figure}
\end{document}

相关内容