The listings
package does not support YAML out of the box. Does a package exist that extends listings
with YAML support? More generally, is there a good listing somewhere of extensions to listings
?
-
2The »minted« package supports YAML indirectly by its back-end Python Pygments.– Thorsten DonigCommented Nov 17, 2013 at 19:57
-
Which yaml? The css framework or the markup language?– knutCommented Nov 17, 2013 at 20:33
-
@knut The markup language. Didn't even know the CSS framework existed!– Sean MackeseyCommented Nov 17, 2013 at 23:22
-
1@ThorstenDonig Thanks Thorsten, this works. You should make it an answer so I can accept it.– Sean MackeseyCommented Nov 17, 2013 at 23:22
-
related: tex.stackexchange.com/questions/152829/…– jub0bsCommented Mar 20, 2014 at 23:12
2 Answers
The »minted« package is your friend here. It uses Python Pygments as back-end for syntax highlighting. And one of the Pygments lexers is for YAML. An approach how to get a YAML script formatted could look like this.
\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{minted}
\begin{document}
\section{The YAML Language}
\begin{minted}[
gobble=4,
frame=single,
linenos
]{yaml}
--- !clarkevans.com/^invoice
invoice: 34843
date : 2001-01-23
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
- sku : BL394D
quantity : 4
description : Basketball
price : 450.00
- sku : BL4438H
quantity : 1
description : Super Hoop
price : 2392.00
tax : 251.42
total: 4443.52
comments: >
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
\end{minted}
\end{document}
For details about the package and further options for customization please refer to its manual.
-
Unfortunately, Pygments highlights very little in the way of YAML syntax, and it is hard to configure otherwise. Using the
listings
package might be a better idea if you want keys and values to be in different colours. Commented May 8, 2017 at 15:48 -
-
Inspired by this answer for JSON, I implemented a similar style with for YAML with the listings package:
\usepackage{xcolor}
\usepackage{listings}
\lstdefinestyle{yaml}{
basicstyle=\color{blue}\footnotesize,
rulecolor=\color{black},
string=[s]{'}{'},
stringstyle=\color{blue},
comment=[l]{:},
commentstyle=\color{black},
morecomment=[l]{-}
}
\begin{lstlisting}[style=yaml]
hello: world
info:
title: hello
version: world
components:
- This is a member
\end{lstlisting}
The result:
It works by setting the default color (basicstyle
) to blue, and then marking everything else as comments. For example,comment=[l]{:}
will mark as a comment the rest of a line ([l]
) after a semicolon ({:}
)