| 1 |
% !TeX program = XeLaTeX |
| 2 |
% !TeX encoding = UTF-8 Unicode |
| 3 |
% |
| 4 |
\documentclass[11pt]{article} |
| 5 |
\frenchspacing |
| 6 |
\setlength\parskip{1.5pt plus 1pt minus 0.5pt} |
| 7 |
\usepackage{fontspec} |
| 8 |
\setmainfont{TeX Gyre Pagella} |
| 9 |
\linespread{1.05}\selectfont |
| 10 |
\setsansfont{TeX Gyre Heros}[ |
| 11 |
Scale=MatchLowercase |
| 12 |
] |
| 13 |
\setmonofont{Fira Code}[ |
| 14 |
Scale=MatchLowercase, |
| 15 |
Contextuals=Alternate |
| 16 |
] |
| 17 |
\newfontfamily\LMMono{Latin Modern Mono}[ |
| 18 |
Scale=MatchLowercase |
| 19 |
] |
| 20 |
\usepackage{microtype} |
| 21 |
\usepackage{xcolor} |
| 22 |
|
| 23 |
\usepackage{listings} |
| 24 |
\usepackage[verbatim]{lstfiracode} |
| 25 |
\lstset{ |
| 26 |
language=C++, |
| 27 |
style=FiraCodeStyle, |
| 28 |
basicstyle=\ttfamily, |
| 29 |
commentstyle=\color{orange} |
| 30 |
} |
| 31 |
\makeatletter |
| 32 |
\lstnewenvironment{LaTeXlisting}{% |
| 33 |
\RestoreVerbatimBehavior |
| 34 |
\lst@CCPutMacro |
| 35 |
\lst@ProcessOther {"2D}{\lst@ttfamily{-{}}{-{}}} |
| 36 |
\@empty\z@\@empty |
| 37 |
\lstset{ |
| 38 |
style={}, |
| 39 |
literate={}, |
| 40 |
language=[LaTeX]TeX, |
| 41 |
basicstyle=\small\LMMono, |
| 42 |
basewidth=0.525em, |
| 43 |
commentstyle=\itshape |
| 44 |
}% |
| 45 |
}{} |
| 46 |
\makeatother |
| 47 |
|
| 48 |
\usepackage{booktabs} |
| 49 |
\usepackage{hyperref} |
| 50 |
\hypersetup{ |
| 51 |
colorlinks=true |
| 52 |
} |
| 53 |
\usepackage{geometry} |
| 54 |
\geometry{ |
| 55 |
width=5.7in |
| 56 |
} |
| 57 |
|
| 58 |
\newcommand*\myemail{[email protected]} |
| 59 |
\newcommand*\dash{} |
| 60 |
\DeclareRobustCommand\dash{% |
| 61 |
\unskip\nobreak\thinspace\textemdash\thinspace\ignorespaces |
| 62 |
} |
| 63 |
\newcommand\versionhistory[3]{% |
| 64 |
% #1: version number, #2: date, #3: description |
| 65 |
\item[#1]#3\hfill#2% |
| 66 |
} |
| 67 |
\title{The \texttt{lstfiracode} package} |
| 68 |
\author{Ruixi~Zhang\thanks{\href{mailto:\myemail}{\nolinkurl{\myemail}}.}} |
| 69 |
\date{2018/12/24\enskip v0.1c} |
| 70 |
|
| 71 |
\begin{document} |
| 72 |
|
| 73 |
\maketitle |
| 74 |
|
| 75 |
\microtypesetup{protrusion=false} |
| 76 |
\tableofcontents |
| 77 |
\microtypesetup{protrusion=true} |
| 78 |
|
| 79 |
\section{Introduction} |
| 80 |
|
| 81 |
The Fira Code\footnote{See \url{https://github.com/tonsky/FiraCode}.} family |
| 82 |
of fonts, created by Nikita Prokopov, is a monospaced typeface with |
| 83 |
programming ligatures. |
| 84 |
It is attempting for many people, me included, to use Fira Code for source |
| 85 |
code listings. |
| 86 |
However, the \verb|lstlisting| environment from the \verb|listings| package |
| 87 |
does not support ligatures naively. To produce the desired output, one must |
| 88 |
specify all necessary ligatures via the \verb|literate| key |
| 89 |
of \verb|\lstset|, which can be tedious. |
| 90 |
|
| 91 |
The \verb|lstfiracode| package defines a ready-to-use \verb|listings| style, |
| 92 |
\verb|FiraCodeStyle|, which pre-specifies 125 ligatures |
| 93 |
(note that Fira Code v1.206 has 130 ligatures in total). |
| 94 |
You may \emph{append} the remaining 5 ligatures to the \verb|FiraCodeStyle| |
| 95 |
literate list via a new key \verb|moreliterate|, without unintentionally |
| 96 |
erasing all existing ligatures via \verb|literate|. |
| 97 |
|
| 98 |
The \verb|lstfiracode| package also provides a package option, |
| 99 |
\verb|verbatim|, along with three switches |
| 100 |
\verb|\ActivateVerbatimLigatures|, \verb|\DeactivateVerbatimLigatures| |
| 101 |
and \verb|\RestoreVerbatimBehavior| |
| 102 |
to support source code listings using Fira Code |
| 103 |
in the \verb|verbatim| environment. |
| 104 |
|
| 105 |
This package does \emph{not} provide the Fira Code font files. |
| 106 |
The newest version of the fonts can be downloaded at |
| 107 |
\url{https://github.com/tonsky/FiraCode/releases}. |
| 108 |
|
| 109 |
\section{Usage} |
| 110 |
|
| 111 |
To access \verb|FiraCodeStyle|, simply load \verb|lstfiracode| \emph{after} |
| 112 |
\verb|listings|. Here is how you may setup your document: |
| 113 |
\begin{LaTeXlisting} |
| 114 |
\documentclass{article} |
| 115 |
\usepackage{fontspec} |
| 116 |
\setmonofont{FiraCode-Regular.otf}[ |
| 117 |
BoldFont=FiraCode-Bold.otf, |
| 118 |
Contextuals=Alternate % Activate the calt feature |
| 119 |
] |
| 120 |
\usepackage{xcolor} |
| 121 |
\usepackage{listings} |
| 122 |
\usepackage[verbatim]{lstfiracode} % Activate ligatures in verbatim |
| 123 |
\lstset{ |
| 124 |
language=C++, |
| 125 |
style=FiraCodeStyle, % Use predefined FiraCodeStyle |
| 126 |
basicstyle=\ttfamily, % Use \ttfamily for source code listings |
| 127 |
commentstyle=\color{orange} |
| 128 |
} |
| 129 |
\begin{document} |
| 130 |
\begin{verbatim} |
| 131 |
A<-www>>=B |
| 132 |
\end{verbatim} |
| 133 |
\begin{lstlisting} |
| 134 |
/* A simple C++ program */ |
| 135 |
int main() { |
| 136 |
cout << "Hello World"; // prints Hello World |
| 137 |
return 0; |
| 138 |
} |
| 139 |
\end{lstlisting} |
| 140 |
\end{document} |
| 141 |
\end{LaTeXlisting} |
| 142 |
which produces the following \verb|verbatim| (observe the {\LMMono<-}, |
| 143 |
the {\LMMono www} and the {\LMMono>>=} ligatures): |
| 144 |
\begin{verbatim} |
| 145 |
A<-www>>=B |
| 146 |
\end{verbatim} |
| 147 |
and the following \verb|lstlisting| (observe the {\LMMono++} |
| 148 |
and the {\LMMono<<} ligatures): |
| 149 |
\begin{lstlisting} |
| 150 |
/* A simple C++ program */ |
| 151 |
int main() { |
| 152 |
cout << "Hello World"; // prints Hello World |
| 153 |
return 0; |
| 154 |
} |
| 155 |
\end{lstlisting} |
| 156 |
|
| 157 |
\section{Package features} |
| 158 |
|
| 159 |
\subsection{Package option and user commands} |
| 160 |
|
| 161 |
The \verb|lstfiracode| package provides one package option and three user |
| 162 |
commands, described below. |
| 163 |
|
| 164 |
You may load the \verb|lstfiracode| package with the option \verb|verbatim|, |
| 165 |
or equivalently \verb|verbatim=true|. This activates all Fira Code ligatures |
| 166 |
in the \verb|verbatim| environment. |
| 167 |
\begin{LaTeXlisting} |
| 168 |
% Activate Fira Code ligatures in verbatim |
| 169 |
\usepackage[verbatim]{lstfiracode} |
| 170 |
% is the same as |
| 171 |
\usepackage[verbatim=true]{lstfiracode} |
| 172 |
\end{LaTeXlisting} |
| 173 |
Loading the package without any option (the default), or equivalently with |
| 174 |
the option \verb|verbatim=false|, \emph{does not alter} how the |
| 175 |
\verb|verbatim| environment is handled. |
| 176 |
\begin{LaTeXlisting} |
| 177 |
% Leave verbatim unaltered |
| 178 |
\usepackage{lstfiracode} |
| 179 |
% is the same as |
| 180 |
\usepackage[verbatim=false]{lstfiracode} |
| 181 |
\end{LaTeXlisting} |
| 182 |
|
| 183 |
You may change your mind in the middle of your document, so there are |
| 184 |
three switches for such purpose: |
| 185 |
\begin{description} |
| 186 |
\item[\texttt{\textbackslash ActivateVerbatimLigatures}] |
| 187 |
Activate all Fira Code ligatures in \verb|verbatim|. This is executed |
| 188 |
automatically with the package option \verb|verbatim=true|. |
| 189 |
\item[\texttt{\textbackslash DeactivateVerbatimLigatures}] |
| 190 |
Suppress \emph{almost all} Fira Code ligatures in \verb|verbatim|. |
| 191 |
Currently, it cannot break the {\LMMono\#\{} and the {\LMMono|\}} ligatures. |
| 192 |
You should use Fira Mono if you wish to avoid ligatures altogether. |
| 193 |
\item[\texttt{\textbackslash RestoreVerbatimBehavior}] |
| 194 |
Restore how \verb|verbatim| is originally handled by \LaTeX. |
| 195 |
\end{description} |
| 196 |
These switches can overwrite each other and they act \emph{locally}. |
| 197 |
For instance, the following three \verb|verbatim| environments |
| 198 |
\begin{LaTeXlisting} |
| 199 |
\begingroup |
| 200 |
\ActivateVerbatimLigatures |
| 201 |
\begin{verbatim} |
| 202 |
A<-www>>=B % Fira Code ligatures activated |
| 203 |
\end{verbatim} |
| 204 |
\DeactivateVerbatimLigatures |
| 205 |
\begin{verbatim} |
| 206 |
A<-www>>=B % Fira Code ligatures deactivated |
| 207 |
\end{verbatim} |
| 208 |
\RestoreVerbatimBehavior |
| 209 |
\begin{verbatim} |
| 210 |
A<-www>>=B % Hmm... |
| 211 |
\end{verbatim} |
| 212 |
\endgroup |
| 213 |
\end{LaTeXlisting} |
| 214 |
produce, respectively, |
| 215 |
\begingroup |
| 216 |
\ActivateVerbatimLigatures |
| 217 |
\begin{verbatim} |
| 218 |
A<-www>>=B % Fira Code ligatures activated |
| 219 |
\end{verbatim} |
| 220 |
\DeactivateVerbatimLigatures |
| 221 |
\begin{verbatim} |
| 222 |
A<-www>>=B % Fira Code ligatures deactivated |
| 223 |
\end{verbatim} |
| 224 |
\RestoreVerbatimBehavior |
| 225 |
\begin{verbatim} |
| 226 |
A<-www>>=B % Hmm... |
| 227 |
\end{verbatim} |
| 228 |
\endgroup |
| 229 |
|
| 230 |
\subsection{The \texttt{FiraCodeStyle} listings style} |
| 231 |
|
| 232 |
The ligatures of Fira Code are treated as literate programming by the |
| 233 |
\verb|lstfiracode| package. These ligatures are specified via the |
| 234 |
\verb|literate| key in defining \verb|FiraCodeStyle|. |
| 235 |
The definition of \verb|FiraCodeStyle| looks like this: |
| 236 |
\begin{LaTeXlisting} |
| 237 |
\lstdefinestyle{FiraCodeStyle}{ |
| 238 |
basewidth=0.6em, |
| 239 |
literate= |
| 240 |
{www}{{www}}3 |
| 241 |
... % All other necessary ligatures in Fira Code |
| 242 |
} |
| 243 |
\end{LaTeXlisting} |
| 244 |
|
| 245 |
Thus, \verb|FiraCodeStyle| specifies \verb|basewidth| explicitly and |
| 246 |
lists \emph{almost all} literate replacements. |
| 247 |
\emph{It does not contain any font changing commands because users may load |
| 248 |
Fira Code according to their preferences.} |
| 249 |
In the case that \verb|\ttfamily| corresponds to Fira Code, |
| 250 |
be sure to specify \verb|basicstyle=\ttfamily| \emph{in addition to} |
| 251 |
\verb|style=FiraCodeStyle|, i.e., |
| 252 |
\begin{LaTeXlisting} |
| 253 |
\usepackage{listings} |
| 254 |
\usepackage{lstfiracode} |
| 255 |
% Assume that you have set Fira Code via \setmonofont |
| 256 |
% Then remember to also specify basicstyle |
| 257 |
\lstset{style=FiraCodeStyle,basicstyle=\ttfamily} |
| 258 |
\end{LaTeXlisting} |
| 259 |
|
| 260 |
\subsection{The missing ligatures and the new key \texttt{moreliterate}} |
| 261 |
|
| 262 |
You may notice that some ligatures in Fira Code are still missing. Well, |
| 263 |
there are 5 such ligatures\dash strictly speaking, they \emph{are} listed |
| 264 |
as literate replacements in the definition of \verb|FiraCodeStyle|, |
| 265 |
but are simply commented out: |
| 266 |
\begin{center} |
| 267 |
\LMMono |
| 268 |
\begin{tabular}{c c c c c} |
| 269 |
\toprule |
| 270 |
\multicolumn{5}{c}{\rmfamily The ``missing'' ligatures} \\ |
| 271 |
\midrule |
| 272 |
/* & */ & // & /// & ;; \\ |
| 273 |
\bottomrule |
| 274 |
\end{tabular} |
| 275 |
\end{center} |
| 276 |
These particular combinations of characters usually indicate comment mode. |
| 277 |
If they were to be implemented as literate replacements, they would break |
| 278 |
how the \verb|listings| package handles comment highlighting. |
| 279 |
|
| 280 |
Nevertheless, you can still \emph{append} these ligatures to the |
| 281 |
\verb|FiraCodeStyle| literate list. |
| 282 |
Say, you want to activate the {\LMMono;;} ligature in your |
| 283 |
{\LMMono C++} code. \emph{But you cannot simply write} |
| 284 |
{\LMMono\textbackslash lstset\{style=FiraCodeStyle,% |
| 285 |
literate=\{;;\}\{\{;;\}\}2\}} |
| 286 |
\emph{because this will erase all predefined ligatures, leaving only the} |
| 287 |
{\LMMono;;} \emph{ligature}. |
| 288 |
Instead, you should use the new key\dash |
| 289 |
\verb|moreliterate|\dash to add more literate replacements: |
| 290 |
\begin{LaTeXlisting} |
| 291 |
% Let's add more ligatures |
| 292 |
\lstset{ |
| 293 |
language=C++, |
| 294 |
style=FiraCodeStyle, |
| 295 |
basicstyle=\ttfamily, |
| 296 |
moreliterate= |
| 297 |
{;;}{{;;}}2 |
| 298 |
{///}{{///}}3 |
| 299 |
} |
| 300 |
\end{LaTeXlisting} |
| 301 |
|
| 302 |
\section{Troubleshooting} |
| 303 |
|
| 304 |
The \verb|lstfiracode| package is maintained at GitHub. Please make each |
| 305 |
bug report with a \emph{minimal example} at |
| 306 |
\url{https://github.com/RuixiZhang42/lstfiracode/issues}. |
| 307 |
Pull requests are welcome. |
| 308 |
|
| 309 |
\section*{Version history} |
| 310 |
|
| 311 |
\begin{description} |
| 312 |
\versionhistory{v0.1c}{2018/12/24}{% |
| 313 |
Removed \texttt{Ligatures=Common} |
| 314 |
from \texttt{README.md} and \texttt{lstfiracode.tex} |
| 315 |
(see \href{https://github.com/RuixiZhang42/lstfiracode/issues/1}{% |
| 316 |
\#1}). |
| 317 |
Re-implemented |
| 318 |
\texttt{\textbackslash DeactivateVerbatimLigatures}.% |
| 319 |
} |
| 320 |
\versionhistory{v0.1b}{2018/12/20}{% |
| 321 |
Updated \texttt{FiraCodeStyle} literate list. |
| 322 |
Added \texttt{\textbackslash RestoreVerbatimBehavior}. |
| 323 |
Re-implemented |
| 324 |
\texttt{\textbackslash Activate}/% |
| 325 |
\texttt{\textbackslash DeactivateVerbatimLigatures}.% |
| 326 |
} |
| 327 |
\versionhistory{v0.1a}{2018/12/16}{% |
| 328 |
Initial release.% |
| 329 |
} |
| 330 |
\end{description} |
| 331 |
|
| 332 |
\end{document} |