一个页面中有多个代码片段

一个页面中有多个代码片段

我有 3 个代码片段,我想使用 Latex 以水平方式显示它们。以便读者可以同时比较所有三个代码。

我尝试使用 minipage 和 paracolumn,但代码变得混乱。

有没有人有更好的想法来做到这一点,或者任何参考都会对我有帮助。

谢谢!

已编辑

我尝试过的代码:

\noindent\fcolorbox{black}{white}{%
\minipage[c]{\dimexpr0.30\linewidth}
   \textcolor{dkgreen}{// Code 1 } \\
   \textcolor{blue}{for} i:=maxint to 0 \textcolor{blue}{do} \\
   \textcolor{blue}{begin} \{  \\
    Sample Code 1 \} \\
   \textcolor{blue}{end}; \\

\endminipage}\hfill
\fcolorbox{black}{white}{%
\minipage[c]{\dimexpr0.30\linewidth}
   \textcolor{dkgreen}{// Code 2 } \\
   \textcolor{blue}{for} i:=maxint to 0 \textcolor{blue}{do} \\
   \textcolor{blue}{begin} \{  \\
   Sample Code 2 \} \\
   \textcolor{blue}{end}; \\
\endminipage}\hfill
 \fcolorbox{black}{white}{%
\minipage[c]{\dimexpr0.30\linewidth}
   \textcolor{dkgreen}{// Code 3 } \\
   \textcolor{blue}{for} i:=maxint to 0 \textcolor{blue}{do} \\
   \textcolor{blue}{begin} \{  \\
   Sample Code 3 \} \\
   \textcolor{blue}{end}; \\
\endminipage} 

在此处输入图片描述

如您所见,我无法正确缩进代码。我还必须自己突出显示该语言的关键字。

答案1

使用listings-package 你可以做这样的事情(我不知道你使用哪种语言,所以我只使用了 python,lstset 取自这里):

\documentclass[]{scrartcl}

\usepackage{listings}
\usepackage{color}
%\lstset{language=Python}
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}
\lstset{ %
  backgroundcolor=\color{white},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}; should come as last argument
  basicstyle=\footnotesize,        % the size of the fonts that are used for the code
  breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
  breaklines=true,                 % sets automatic line breaking
  captionpos=b,                    % sets the caption-position to bottom
  commentstyle=\color{mygreen},    % comment style
  deletekeywords={...},            % if you want to delete keywords from the given language
  escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
  extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
  frame=single,                    % adds a frame around the code
  keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  keywordstyle=\color{blue},       % keyword style
  language=Python,                 % the language of the code
  morekeywords={*,...},           % if you want to add more keywords to the set
  numbers=left,                    % where to put the line-numbers; possible values are (none, left, right)
  numbersep=5pt,                   % how far the line-numbers are from the code
  numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers
  rulecolor=\color{black},         % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
  showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
  showstringspaces=false,          % underline spaces within strings only
  showtabs=false,                  % show tabs within strings adding particular underscores
  stepnumber=2,                    % the step between two line-numbers. If it's 1, each line will be numbered
  stringstyle=\color{mymauve},     % string literal style
  tabsize=2,                       % sets default tabsize to 2 spaces
  title=\lstname                   % show the filename of files included with \lstinputlisting; also try caption instead of title
}

\begin{document}
\noindent
\begin{minipage}[t]{0.3\textwidth}
    \begin{lstlisting}[frame=single]
i=0
while i < 5:
    example_code1
    example_code2
    example_code3
    i++
return
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}[t]{0.3\textwidth}
\begin{lstlisting}[frame=single]
i=0
while i < 5:
    example_code
    i++
return
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}[t]{0.3\textwidth}
\begin{lstlisting}[frame=single]
i=0
while i < 5:
    example_code1
    example_code2
    i++
return
\end{lstlisting}
\end{minipage}

\end{document}

编辑:添加了缺失的\noindent结果

答案2

verbatimbox包允许将代码片段保存在\hboxes 中,以便以后调用,在本例中是通过\hfilled 行调用。

\documentclass[a4paper]{article}

\usepackage[margin=1cm]{geometry}
\usepackage{verbatimbox,stackengine}
\begin{document}
\begin{myverbbox}[\scriptsize]{\codeA}
long some_function();
/* int */ other_function();

/* int */ calling_function()
{
    long test1;
    register /* int */ test2;

    test1 = some_function();
    if (test1 > 0)
          test2 = 0;
    else
          test2 = other_function();
    return test2;
}
\end{myverbbox}
\begin{myverbbox}[\scriptsize]{\codeB}
#include <stdio.h>

int main(void)
{
    printf("hello, world\n");
}
\end{myverbbox}
\begin{myverbbox}[\scriptsize]{\codeC}
#include <stdio.h>

main()
{
   int number;

   printf("Enter an integer\n");
   scanf("%d",&number);

   printf("Integer entered by you is
     %d\n", number);

   return 0;
}
\end{myverbbox}

\noindent\belowbaseline[0pt]{\codeA}\hfill%
\belowbaseline[0pt]{\codeB}\hfill%
\belowbaseline[0pt]{\codeC}
\end{document}

在此处输入图片描述

也可以\codeA\fboxes 中呈现 等:

在此处输入图片描述

相关内容