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.
Info |
---|
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.
Expand | |||||
---|---|---|---|---|---|
| |||||
The default input expression is \inputminted [ options ]{ language }{ filename }or
|
Minted may also be applied to a single word or phrase in the middle of a text.
Expand | ||
---|---|---|
| ||
The default expression for mintinline is \mintinline[ options ]{ language }{ code }
|
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.
Code Block |
---|
\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.
Code Block |
---|
\inputminted[fontsize=\default]{C++}{isPrime.cpp} \inputminted[fontsize=\footnotesize]{C++}{isPrime.cpp} |
Line breaks
To automatically break lines that are too long, include the option breaklines.
Code Block |
---|
\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.
Code Block |
---|
\inputminted[linenos]{C++}{isPrime.cpp} \inputminted[linenos, firstnumber=last]{C++}{Linenumber.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.
Code Block |
---|
\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.
Code Block |
---|
\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. Different styles may be found at the Overleaf documentation linked above.
Code Block |
---|
\usemintedstyle{borland} \inputminted{C++}{isPrime.cpp} \usemintedstyle{rrt} \inputminted{C++}{isPrime.cpp} |
BibTeX Display Table |
---|