It is possible to pull the source code in LaTeX from a file using the command \lstinputlisting[language=LANGUAGENAME]{FILE NAME}. This command also lets you choose which language the code will be in. If you wish to import only part of the source file, you can mention which lines of code to include.
Syntax highlighting is a feature of text editors that are used for programming, scripting, or markup languages, such as HTML. The feature displays text, especially source code, in different colours and fonts according to the category of terms.
The package allows you to enter Python code within a LaTeX document, execute the code, and access its output in the original document. There is also support for Bash, JavaScript, Julia, Octave, Perl, R, Raku (Perl 6), Ruby, Rust, and SageMath.
Taking Norman’s advice to heart, I’ve hacked together a solution that used (a patched) Pygments for highlighting and pushed in as many features as possible without bursting ;-)
I’ve also created a LateX package, once my Pygments patch was released in version 1.2 …
minted is a package that uses Pygments to provide top-notch syntax highlighting in LaTeX. For example, it allows the following output.
Here’s a minimal file to reproduce the above code (notice that including Unicode characters might require XeTeX)!
\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{minted}
\setsansfont{Calibri}
\setmonofont{Consolas}
\begin{document}
\renewcommand{\theFancyVerbLine}{
\sffamily\textcolor[rgb]{0.5,0.5,0.5}{\scriptsize\arabic{FancyVerbLine}}}
\begin{minted}[mathescape,
linenos,
numbersep=5pt,
gobble=2,
frame=lines,
framesep=2mm]{csharp}
string title = "This is a Unicode π in the sky"
/*
Defined as $\pi=\lim_{n\to\infty}\frac{P_n}{d}$ where $P$ is the perimeter
of an $n$-sided regular polygon circumscribing a
circle of diameter $d$.
*/
const double pi = 3.1415926535
\end{minted}
\end{document}
This can be typeset using the following command:
xelatex -shell-escape test.tex
(But minted also works with latex
and pdflatex
…)
minted.sty
works similar to texments.sty
but allows additional features.
minted is listed on CTAN (package info)
documentation is of course included.
minted is now maintained by Geoffrey Poore. The development version, including the latest .sty
file, is available at github.com/gpoore/minted, and can be cloned from there.
Once again, thanks to Norman for motivating me to produce this package.
TeX is (famously) Turing-complete, but I'm pretty sure you're going to have to write this extension yourself. The documentation makes it clear that the original author of listings
orphaned it in 2004, and that it has not been updated since 2006. The package wasn't designed to override the formatting of numeric literals, but you might be able to modify it by changing the definition of \lst@ProcessDigit
. If that doesn't work, you'll have to understand in detail how the "identifier style" options work, and you'll have to duplicate that machinery for your numeric literals.
I don't quite understand why you're so reluctant to introduce an external tool into your toolchain, but because you are, you'll have to do extra work. After a look at the source code, I expect that modifying listings
should be possible, but I personally would choose to preprocess my LaTeX instead.
This is a package which converts source code to TeX and LaTeX with syntax highlighting. It is possible to add custom programming language definitions easily.
Highlight supports 100 programming languages and includes 50 colour themes. It features code reformatting and indenting.
I haven't used it to know how WELL it works, but someone else I know said it worked pretty well. If I get a chance to, I'll try it out myself and see.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With