我正在尝试对一段 Ruby 和 Java 代码的 listing-mode 和 minted-mode 的结果进行并排比较。我无法让它们并排显示。有什么建议可以让编程代码并排显示吗?
% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode
% This is a simple template for a LaTeX document using the "article" class.
% See "book", "report", "letter" for other types of document.
\documentclass{article} % use larger type; default would be 10pt
\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)
%%% Examples of Article customizations
% These packages are optional, depending whether you want the features they provide.
% See the LaTeX Companion or other references for full information.
%%% PAGE DIMENSIONS
\usepackage{geometry} % to change the page dimensions
\geometry{a4paper} % or letterpaper (US) or a5paper or....
% \geometry{margin=2in} % for example, change the margins to 2 inches all round
% \geometry{landscape} % set up the page for landscape
% read geometry.pdf for detailed page layout information
\usepackage{graphicx} % support the \includegraphics command and options
% \usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent
%%% PACKAGES
\usepackage{booktabs} % for much better looking tables
\usepackage{array} % for better arrays (eg matrices) in maths
\usepackage{paralist} % very flexible & customisable lists (eg. enumerate/itemize, etc.)
\usepackage{verbatim} % adds environment for commenting out blocks of text & for better verbatim
\usepackage{subfig} % make it possible to include more than one
% captioned figure/table in a single float
\usepackage{multicol}
\usepackage{xcolor}
\usepackage{minted}
\usepackage{listings}
\lstloadlanguages{Ruby}
\lstloadlanguages{Java}
\lstset{%
basicstyle=\ttfamily\color{black},
commentstyle = \ttfamily\color{red},
keywordstyle=\ttfamily\color{blue},
stringstyle=\color{orange}}
% These packages are all incorporated in the memoir class to one degree or another...
%%% HEADERS & FOOTERS
\usepackage{fancyhdr} % This should be set AFTER setting up the page geometry
\pagestyle{fancy} % options: empty , plain , fancy
\renewcommand{\headrulewidth}{0pt} % customise the layout...
\lhead{}\chead{}\rhead{}
\lfoot{}\cfoot{\thepage}\rfoot{}
%%% SECTION TITLE APPEARANCE
\usepackage{sectsty}
\allsectionsfont{\sffamily\mdseries\upshape} % (See the fntguide.pdf for font help)
% (This matches ConTeXt defaults)
%%% ToC (table of contents) APPEARANCE
\usepackage[nottoc,notlof,notlot]{tocbibind} % Put the bibliography in the ToC
\usepackage[titles,subfigure]{tocloft} % Alter the style of the Table of Contents
\renewcommand{\cftsecfont}{\rmfamily\mdseries\upshape}
\renewcommand{\cftsecpagefont}{\rmfamily\mdseries\upshape} % No bold!
%%% END Article customizations
%%% The "real" document content comes below...
\title{Verschil listing of minted syntax hightlighting \\ Een onderzoekje}
\author{ir. G.M. Borkent}
%\date{} % Activate to display a given date or no date (if empty),
% otherwise the current date is printed
\begin{document}
\maketitle
\section{Verschil tussen listing en minted syntax highlighting}
\subsection{Vergelijking Ruby}
\begin{minipage}[b]{0.5\linewidth}
\centering
Listing-mode:
\begin{lstlisting}[language=Ruby]
#this is a comment
a = 5
b = a * 5
puts b
puts b + 3
class Test < Test::SomeClass
@test = 3
def bar
if foo
puts "foo"
else
puts "bar"
end
end
end
\end{lstlisting}
\end{minipage}
\begin{minipage}[b]{0.5\linewidth}
\centering
\noindent
Minted-mode:
\begin{minted}{ruby}
#this is a comment
a = 5
b = a * 5
puts b
puts b + 3
class Test < Test::SomeClass
@test = 3
def bar
if foo
puts "foo"
else
puts "bar"
end
end
end
\end{minted}
\end{minipage}
\subsection{Vergelijking Java}
\begin{minipage}[b]{0.5\linewidth}
\centering
\noindent
Listing-mode
\begin{lstlisting}[language=Java]
public static void main(String[] args) {
int a = 3;
}
\end{lstlisting}
\end{minipage}
\begin{minipage}[b]{0.5\linewidth}
\centering
\noindent
Minted-mode
\begin{minted}{java}
public static void main(String[] args) {
int a = 3;
}
\end{minted}
\end{minipage}
\section{Conclusie}
Minted-mode ziet er mooier uit\ldots
\end{document}
答案1
您需要允许换行符(选项breaklines
)listings
,使用较小的字体大小以适合文本\columnwidth
,或者直接缩放代码环境以\columnwidth
使用\Resizebox{\columnwidth}{!}{<content>}
(realboxes
包)或\begin{adjustbox}{width=\columnwidth} ... \end{adjustbox}
(adjustbox
包)。请注意,常规\scalebox
或\resizebox
不适用于逐字内容。
编辑:
似乎minted
环境不能保存在盒子里(我收到一个Something's wrong--perhaps a missing \item.
错误),这破坏了adjustbox
。我之前已经指出, 的 LaTeX 部分minted
实现得不是很好。查看代码时,我甚至看到引入了不需要的空格,这是一种初学者错误。您可能只想降低字体大小直到适合,例如\scriptsize
在代码环境周围使用。
答案2
虽然它不会自动并排,但使用一个 minpage 包装另外两个包含 minted 环境的小页面,是实现此目的的最简单方法。
示例仅包含相关行,没有样式:
\begin{minipage}[]{\linewidth}
\footnotesize
\begin{minipage}[]{0.60\linewidth}
\paragraph{Code mit Branching}
\label{lst:branching_code}
\inputminted[firstline=43,lastline=48]{c++}{code/Minimax.cpp}
\paragraph{X86 Assembly}
\inputminted{asm}{code/branching_other_function.s}
\end{minipage}
\hfill
\begin{minipage}[]{0.35\linewidth}
\paragraph{Branchless Code}
\label{lst:branchless_code}
\inputminted{c++}{code/Minimax_perf.cpp}
\vspace{1.1cm}
\paragraph{X86 Assembly}
\inputminted{asm}{code/branchless_other_function.s}
\vspace{3.6cm}
\end{minipage}
\end{minipage}