There are several methods for importing code to your project. We will here provide you with some of our different methods and styles, hopefully enabeling you to find or create the one you enjoy the most. As always, keep in mind that there are many other good tutorials online, covering both the methods we explain here, but also other methods. To improve the most, we highly recommend you to check them out as well!


The example code used on this page is written in C++, but the difference is negligible for other languages.

Page content

Minted

Minted is a package that allows formatting and highlighting source code in LaTeX. Some minted documentation may be found at Overleaf or through the original package documentation. Most of the information we provide will be obtained from the package documentation, but given the number of options available, we will only cover a minor part.

Remember to include the Minted package: \usepackage{minted}


We will only use the option of including code through external files, but you may also include code directly. An example is shown below.

The default input expression is \inputminted [ options ]{ language }{ filename } or

% Method 1 - Assumed that the code included in 'isPrime.cpp' is the same as in Method 2 
\inputminted{C++}{isPrime.cpp} 


% Method 2 
\begin{minted}{C++} 
#include <iostream>

bool isPrime() { 
	int number; 
	std::string question = "Please enter a number: "; 

	std::cout << question << std::endl; 
	std::cin >> number; 

	for (int i = 2; i < number; i++) { 
		if (number % i == 0) // If prime number 
			return false; 
	} 
	return true; 
} 
\end{minted}

Minted may also be applied to a single word or phrase in the middle of a text.

The default expression for mintinline is \mintinline[ options ]{ language }{ code }

The line \mintinline{C++}{#include iostream} is often found at the top of a file.

Background color

To change the background color for the code, we may use the option bgcolor with predefined or custom colors. Note that there are several limitations to this option, further described in the package documentation.

\inputminted[bgcolor=red]{C++}{isPrime.cpp}

\definecolor{my-bg}{rgb}{0.95,0.95,0.95}
\inputminted[bgcolor=my-bg]{C++}{isPrime.cpp}

Font size

If importing a lot of code to your project, it is often beneficial to decrease the font size. This will reduce the space required, but still keep the code readable. To change the font size, use the option fontsize together with the regular LaTeX font sizes.

\inputminted[fontsize=\default]{C++}{isPrime.cpp}

\inputminted[fontsize=\footnotesize]{C++}{isPrime.cpp}

Frame

To insert a frame around your code, use the option frame alongside the types leftline, rightline, topline, bottomline, lines, single.

\inputminted[frame=leftline]{C++}{Frame.cpp}

\inputminted[frame=single]{C++}{Frame.cpp}

\inputminted[frame=lines]{C++}{Frame.cpp}

Width of frame

The width of the frame is by default set to 0.4pt. This can however be customized using the option framerule.

\inputminted[frame=single]{C++}{Frame.cpp}

\inputminted[frame=single, framerule=1pt]{C++}{Frame.cpp}

Distance between frame and code

The distance between the frame and the code can be set using the option framesep. It is by default defined as \fboxsep, which is equal to 3pt.

\inputminted[frame=single]{C++}{Frame.cpp}

\inputminted[frame=single, framesep=15pt]{C++}{Frame.cpp}

Frame color

The frame color can be defined by the option rulecolor.

\inputminted[frame=single, rulecolor=purple]{C++}{Frame.cpp}

Line breaks

To automatically break lines that are too long, include the option breaklines.

\inputminted[breaklines]{C++}{Breaklines.cpp}

\inputminted{C++}{Breaklines.cpp}

Line numbering

To add line numbering to your code, include the option linenos

Customized numbering

Using the option firstnumber we can specify where to start our numbering. This can for example be used to continue the numbering from where you last stopped.

\inputminted[linenos]{C++}{isPrime.cpp}

\inputminted[linenos, firstnumber=last]{C++}{Linenumber.cpp}


Location of numbers

To choose the side you want the numbers to appear on, use the option numbers alongside the parameteres left, right or both.

\inputminted[numbers=both]{C++}{isPrime.cpp}

Numbering blank lines

Use the option numberblanklines to choose if you want to number blank lines or not.

\inputminted[linenos, numberblanklines=false]{C++}{isPrime.cpp}

Distance between numbers and code

To change the distance between the numbers and the code, use the option numbersep. It is by default defined as 12pt.

\inputminted[linenos]{C++}{isPrime.cpp}

\inputminted[linenos, numbersep=2pt]{C++}{isPrime.cpp}

Macro options

Using the commands \setminted[ language ]{ key=value,... } and  \setmintedinline[ language ]{ key=value,... }, you may define general options for the entire document or for a specific language. However, individual options will still override the options set for the document or a language. The command is preferably included outside the document (in the setup), but may also be included inside.

\setminted[C++]{breaklines=true, mathescape=true, linenos=true}

\inputminted{C++}{Math.cpp}

Mathematical symbols

Using the option mathescape, Minted has given us the possibility to include mathematical symbols within our code.

\inputminted[mathescape]{C++}{Math.cpp}


% --- Math.cpp ---
% Calculate factorial of positive number
% The factorial of 'n' is defined as $n! = \prod_{i = 1}^n i$
int factorial(int n) {
    int sum = 1;
    while (n != 1) {
        sum = sum * n--;
    }
    return sum;
}

Styles

Minted offers a number of different styles. To change the style, include the command \usemintedstyle( style ) when including the package Minted or use the option style. Different styles may be found at the Overleaf documentation linked above.

\usemintedstyle{borland}
\inputminted{C++}{isPrime.cpp}

\inputminted[style=rrt]{C++}{isPrime.cpp}

Example setups

To give you some inspiration, we have made some example setups. Use these to sort out what you prefer or don't perfer, and combine the best to make your own personal setup.

Minted

Example 1

% Setup
\definecolor{my-bg}{rgb}{0.95,0.95,0.95}
\setminted[C++]{
    breaklines=true, 
    mathescape=true, 
    linenos=true,
    numbersep=5pt,
    bgcolor=my-bg
	}

% All code imported will be given the options defined in setup unless specified otherwise
\inputminted{C++}{isPrime.cpp}

Example 2

% Setup
\setminted[C++]{
    breaklines=true, 
    mathescape=true, 
    fontsize=\footnotesize,
    linenos=true,
    numberblanklines=false,
    frame=leftline,
    framerule=0.8pt,
    style=tango
    }
    
% All code imported will be given the options defined in setup unless specified otherwise
\inputminted{C++}{isPrime.cpp}

Example 3

% Setup
\setminted[C++]{
    breaklines=true, 
    mathescape=true, 
    bgcolor=black,
    style=monokai
    }
    
% All code imported will be given the options defined in setup unless specified otherwise
\inputminted{C++}{isPrime.cpp}



  • No labels