4

I have 3 code snippets which I want to show in a horizontal way using Latex. So that the reader can compare all the three codes at the same time.

I tried using minipage, paracolumn but the code gets distorted.

Does anyone have a better idea to do this, or any reference will be helpful to me.

Thanks!

Edited

Code I tried :

\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} 

enter image description here

As you can see I cannot indent the code properly. Also i have to highlight the keywords for the language myself.

  • I don't get what your output should look like. Could you please add an image in which you show us what you want. And please add a MWE of what you tried so far. – Skillmon Mar 17 '17 at 11:11
  • there is no reason why three listings or minted code blocks in three minipages should be distorted or not come side by side. If it did not work you presumably had an error in your code, but if you want help with that, it is best to show your code. – David Carlisle Mar 17 '17 at 11:31
  • David and @Skillmon Code and output added. – TechJ Mar 17 '17 at 11:37
5

Using the listings-package you could do something like this (I didn't know which language you're using, so I just used python, the lstset is taken from here):

\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}

EDIT: added a missing \noindent. results

| improve this answer | |
4

The verbatimbox package allows code snippets to be saved in \hboxes that can be later recalled, in this case across an \hfilled line.

\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}

enter image description here

One can present \codeA, etc. in \fboxes, as well:

enter image description here

| improve this answer | |
  • Thank you steven for your help. Can I use minted or listings in the same way to highlight my code keywords etc. – TechJ Mar 17 '17 at 11:38
  • @TechJ verbatimbox does not perform code highlighting, I am sorry to say. For that, you should take David's suggestion to use listings or minted in minipages. – Steven B. Segletes Mar 17 '17 at 11:40
  • Ok for that I will try again. Meanwhile for the code that you have provided works fine but it does not formats the tab/indentation for me. I copied exactly the same code, for me all the lines of code snippet starts from the same point. Do you have any idea about it, or is it something wrong on my side. – TechJ Mar 17 '17 at 11:45
  • @TechJ Your example code is done with \texttt, which is not the same as verbatim text. In general, to get spaces, as well as special characters like $, %, &, \ , you need to use verbatim text. – Steven B. Segletes Mar 17 '17 at 11:49
  • 1
    @TechJ If you are executing my exact code and get a different looking result, perhaps some of your packages are out of date. – Steven B. Segletes Mar 17 '17 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.