summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/lstfiracode/lstfiracode.tex
blob: e40f1583c7d26d224fedb780be5615f9c78e0686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
% !TeX program = XeLaTeX
% !TeX encoding = UTF-8 Unicode
%
\documentclass[11pt]{article}
\frenchspacing
\setlength\parskip{1.5pt plus 1pt minus 0.5pt}
\usepackage{fontspec}
\setmainfont{TeX Gyre Pagella}
\linespread{1.05}\selectfont
\setsansfont{TeX Gyre Heros}[
  Scale=MatchLowercase
]
\setmonofont{Fira Code}[
  Scale=MatchLowercase,
  Contextuals=Alternate
]
\newfontfamily\LMMono{Latin Modern Mono}[
  Scale=MatchLowercase
]
\usepackage{microtype}
\usepackage{xcolor}

\usepackage{listings}
\usepackage[verbatim]{lstfiracode}
\lstset{
  language=C++,
  style=FiraCodeStyle,
  basicstyle=\ttfamily,
  commentstyle=\color{orange}
}
\makeatletter
\lstnewenvironment{LaTeXlisting}{%
  \RestoreVerbatimBehavior
  \lst@CCPutMacro
    \lst@ProcessOther {"2D}{\lst@ttfamily{-{}}{-{}}}
    \@empty\z@\@empty
  \lstset{
    style={},
    literate={},
    language=[LaTeX]TeX,
    basicstyle=\small\LMMono,
    basewidth=0.525em,
    commentstyle=\itshape
  }%
}{}
\makeatother

\usepackage{booktabs}
\usepackage{hyperref}
\hypersetup{
  colorlinks=true
}
\usepackage{geometry}
\geometry{
  width=5.7in
}

\newcommand*\myemail{ruixizhang42@gmail.com}
\newcommand*\dash{}
\DeclareRobustCommand\dash{%
  \unskip\nobreak\thinspace\textemdash\thinspace\ignorespaces
}
\newcommand\versionhistory[3]{%
  % #1: version number, #2: date, #3: description
  \item[#1]#3\hfill#2%
}
\title{The \texttt{lstfiracode} package}
\author{Ruixi~Zhang\thanks{\href{mailto:\myemail}{\nolinkurl{\myemail}}.}}
\date{2018/12/24\enskip v0.1c}

\begin{document}

\maketitle

\microtypesetup{protrusion=false}
\tableofcontents
\microtypesetup{protrusion=true}

\section{Introduction}

The Fira Code\footnote{See \url{https://github.com/tonsky/FiraCode}.} family
of fonts, created by Nikita Prokopov, is a monospaced typeface with
programming ligatures.
It is attempting for many people, me included, to use Fira Code for source
code listings.
However, the \verb|lstlisting| environment from the \verb|listings| package
does not support ligatures naively. To produce the desired output, one must
specify all necessary ligatures via the \verb|literate| key
of \verb|\lstset|, which can be tedious.

The \verb|lstfiracode| package defines a ready-to-use \verb|listings| style,
\verb|FiraCodeStyle|, which pre-specifies 125 ligatures
(note that Fira Code v1.206 has 130 ligatures in total).
You may \emph{append} the remaining 5 ligatures to the \verb|FiraCodeStyle|
literate list via a new key \verb|moreliterate|, without unintentionally
erasing all existing ligatures via \verb|literate|.

The \verb|lstfiracode| package also provides a package option,
\verb|verbatim|, along with three switches
\verb|\ActivateVerbatimLigatures|, \verb|\DeactivateVerbatimLigatures|
and \verb|\RestoreVerbatimBehavior|
to support source code listings using Fira Code
in the \verb|verbatim| environment.

This package does \emph{not} provide the Fira Code font files.
The newest version of the fonts can be downloaded at
\url{https://github.com/tonsky/FiraCode/releases}.

\section{Usage}

To access \verb|FiraCodeStyle|, simply load \verb|lstfiracode| \emph{after}
\verb|listings|. Here is how you may setup your document:
\begin{LaTeXlisting}
    \documentclass{article}
    \usepackage{fontspec}
    \setmonofont{FiraCode-Regular.otf}[
      BoldFont=FiraCode-Bold.otf,
      Contextuals=Alternate  % Activate the calt feature
    ]
    \usepackage{xcolor}
    \usepackage{listings}
    \usepackage[verbatim]{lstfiracode} % Activate ligatures in verbatim
    \lstset{
      language=C++,
      style=FiraCodeStyle,   % Use predefined FiraCodeStyle
      basicstyle=\ttfamily,  % Use \ttfamily for source code listings
      commentstyle=\color{orange}
    }
    \begin{document}
    \begin{verbatim}
    A<-www>>=B
    \end{verbatim}
    \begin{lstlisting}
    /* A simple C++ program */
    int main() {
        cout << "Hello World"; // prints Hello World
        return 0;
    }
    \end{lstlisting}
    \end{document}
\end{LaTeXlisting}
which produces the following \verb|verbatim| (observe the {\LMMono<-},
the {\LMMono www} and the {\LMMono>>=} ligatures):
\begin{verbatim}
    A<-www>>=B
\end{verbatim}
and the following \verb|lstlisting| (observe the {\LMMono++}
and the {\LMMono<<} ligatures):
\begin{lstlisting}
    /* A simple C++ program */
    int main() {
        cout << "Hello World"; // prints Hello World
        return 0;
    }
\end{lstlisting}

\section{Package features}

\subsection{Package option and user commands}

The \verb|lstfiracode| package provides one package option and three user
commands, described below.

You may load the \verb|lstfiracode| package with the option \verb|verbatim|,
or equivalently \verb|verbatim=true|. This activates all Fira Code ligatures
in the \verb|verbatim| environment.
\begin{LaTeXlisting}
    % Activate Fira Code ligatures in verbatim
    \usepackage[verbatim]{lstfiracode}
    % is the same as
    \usepackage[verbatim=true]{lstfiracode}
\end{LaTeXlisting}
Loading the package without any option (the default), or equivalently with
the option \verb|verbatim=false|, \emph{does not alter} how the
\verb|verbatim| environment is handled.
\begin{LaTeXlisting}
    % Leave verbatim unaltered
    \usepackage{lstfiracode}
    % is the same as
    \usepackage[verbatim=false]{lstfiracode}
\end{LaTeXlisting}

You may change your mind in the middle of your document, so there are
three switches for such purpose:
\begin{description}
\item[\texttt{\textbackslash ActivateVerbatimLigatures}]
Activate all Fira Code ligatures in \verb|verbatim|. This is executed
automatically with the package option \verb|verbatim=true|.
\item[\texttt{\textbackslash DeactivateVerbatimLigatures}]
Suppress \emph{almost all} Fira Code ligatures in \verb|verbatim|.
Currently, it cannot break the {\LMMono\#\{} and the {\LMMono|\}} ligatures.
You should use Fira Mono if you wish to avoid ligatures altogether.
\item[\texttt{\textbackslash RestoreVerbatimBehavior}]
Restore how \verb|verbatim| is originally handled by \LaTeX.
\end{description}
These switches can overwrite each other and they act \emph{locally}.
For instance, the following three \verb|verbatim| environments
\begin{LaTeXlisting}
    \begingroup
    \ActivateVerbatimLigatures
    \begin{verbatim}
    A<-www>>=B % Fira Code ligatures activated
    \end{verbatim}
    \DeactivateVerbatimLigatures
    \begin{verbatim}
    A<-www>>=B % Fira Code ligatures deactivated
    \end{verbatim}
    \RestoreVerbatimBehavior
    \begin{verbatim}
    A<-www>>=B % Hmm...
    \end{verbatim}
    \endgroup
\end{LaTeXlisting}
produce, respectively,
\begingroup
\ActivateVerbatimLigatures
\begin{verbatim}
    A<-www>>=B % Fira Code ligatures activated
\end{verbatim}
\DeactivateVerbatimLigatures
\begin{verbatim}
    A<-www>>=B % Fira Code ligatures deactivated
\end{verbatim}
\RestoreVerbatimBehavior
\begin{verbatim}
    A<-www>>=B % Hmm...
\end{verbatim}
\endgroup

\subsection{The \texttt{FiraCodeStyle} listings style}

The ligatures of Fira Code are treated as literate programming by the
\verb|lstfiracode| package. These ligatures are specified via the
\verb|literate| key in defining \verb|FiraCodeStyle|.
The definition of \verb|FiraCodeStyle| looks like this:
\begin{LaTeXlisting}
    \lstdefinestyle{FiraCodeStyle}{
      basewidth=0.6em,
      literate=
        {www}{{www}}3
        ...  % All other necessary ligatures in Fira Code
    }
\end{LaTeXlisting}

Thus, \verb|FiraCodeStyle| specifies \verb|basewidth| explicitly and
lists \emph{almost all} literate replacements.
\emph{It does not contain any font changing commands because users may load
Fira Code according to their preferences.}
In the case that \verb|\ttfamily| corresponds to Fira Code,
be sure to specify \verb|basicstyle=\ttfamily| \emph{in addition to}
\verb|style=FiraCodeStyle|, i.e.,
\begin{LaTeXlisting}
    \usepackage{listings}
    \usepackage{lstfiracode}
    % Assume that you have set Fira Code via \setmonofont
    % Then remember to also specify basicstyle
    \lstset{style=FiraCodeStyle,basicstyle=\ttfamily}
\end{LaTeXlisting}

\subsection{The missing ligatures and the new key \texttt{moreliterate}}

You may notice that some ligatures in Fira Code are still missing. Well,
there are 5 such ligatures\dash strictly speaking, they \emph{are} listed
as literate replacements in the definition of \verb|FiraCodeStyle|,
but are simply commented out:
\begin{center}
\LMMono
\begin{tabular}{c c c c c}
\toprule
\multicolumn{5}{c}{\rmfamily The ``missing'' ligatures} \\
\midrule
/* & */ & // & /// & ;; \\
\bottomrule
\end{tabular}
\end{center}
These particular combinations of characters usually indicate comment mode.
If they were to be implemented as literate replacements, they would break
how the \verb|listings| package handles comment highlighting.

Nevertheless, you can still \emph{append} these ligatures to the
\verb|FiraCodeStyle| literate list.
Say, you want to activate the {\LMMono;;} ligature in your
{\LMMono C++} code. \emph{But you cannot simply write}
{\LMMono\textbackslash lstset\{style=FiraCodeStyle,%
literate=\{;;\}\{\{;;\}\}2\}}
\emph{because this will erase all predefined ligatures, leaving only the}
{\LMMono;;} \emph{ligature}.
Instead, you should use the new key\dash
\verb|moreliterate|\dash to add more literate replacements:
\begin{LaTeXlisting}
    % Let's add more ligatures
    \lstset{
      language=C++,
      style=FiraCodeStyle,
      basicstyle=\ttfamily,
      moreliterate=
        {;;}{{;;}}2
        {///}{{///}}3
    }
\end{LaTeXlisting}

\section{Troubleshooting}

The \verb|lstfiracode| package is maintained at GitHub. Please make each
bug report with a \emph{minimal example} at
\url{https://github.com/RuixiZhang42/lstfiracode/issues}.
Pull requests are welcome.

\section*{Version history}

\begin{description}
\versionhistory{v0.1c}{2018/12/24}{%
  Removed \texttt{Ligatures=Common}
    from \texttt{README.md} and \texttt{lstfiracode.tex}
    (see \href{https://github.com/RuixiZhang42/lstfiracode/issues/1}{%
    \#1}).
  Re-implemented
    \texttt{\textbackslash DeactivateVerbatimLigatures}.%
}
\versionhistory{v0.1b}{2018/12/20}{%
  Updated \texttt{FiraCodeStyle} literate list.
  Added \texttt{\textbackslash RestoreVerbatimBehavior}.
  Re-implemented
    \texttt{\textbackslash Activate}/%
    \texttt{\textbackslash DeactivateVerbatimLigatures}.%
}
\versionhistory{v0.1a}{2018/12/16}{%
  Initial release.%
}
\end{description}

\end{document}