| 1 |
# Highlight LaTeX: Pretty LaTeX code within LaTeX |
| 2 |
|
| 3 |
Teaching other people LaTeX is great fun, and I've seen plenty of slides and |
| 4 |
readers doing so. You show what you achieve, and how you achieve it. However, |
| 5 |
while the LaTeX you achieve might look splendid, showing code is often done |
| 6 |
using very rudimentary solutions (like plain old verbatim). |
| 7 |
|
| 8 |
They're not to blame: I didn't find pretty colored LaTeX highlighting either. I |
| 9 |
sure want it, and if you're reading this, you probably too, so I've made a |
| 10 |
package for it! It builds further on the generic `listings` package to provide |
| 11 |
colored highlighting for LaTeX. The file `demo.tex` achieves this: |
| 12 |
|
| 13 |
<img src="assets/demoshowoff.png" width="600" |
| 14 |
alt="highlight demo" title="Highlight demo"> |
| 15 |
|
| 16 |
## Manual installation |
| 17 |
|
| 18 |
1. Download the `.sty` file, e.g. |
| 19 |
[right click here and select 'save as'](https://github.com/vkuhlmann/highlight-latex/raw/main/highlightlatex.sty). |
| 20 |
2. Copy the file to the directory where your `.tex` file is at... |
| 21 |
3. ... or make it available for all your `.tex` files by placing the file in |
| 22 |
your TEXMF root. |
| 23 |
|
| 24 |
**Locating TEXTMF root** |
| 25 |
|
| 26 |
Note the following default locations: |
| 27 |
|
| 28 |
MiKTeX on Windows: |
| 29 |
global installation: C:\Program Files\MiKTeX\tex\latex |
| 30 |
local installation: %AppData%\..\Local\Programs\MiKTeX\tex\latex |
| 31 |
|
| 32 |
TeX Live on Linux or Mac: |
| 33 |
/usr/share/texmf-texlive/tex/latex |
| 34 |
/usr/local/texlive/texmf-local/tex/latex |
| 35 |
|
| 36 |
On Windows you can open a path by pasting the address in the Windows Explorer |
| 37 |
address bar, or pressing <kbd>Start</kbd>+<kbd>R</kbd> and pasting the |
| 38 |
address there. One of these directories should contain a long list of |
| 39 |
packages you have installed. If you can't it, and you use MiKTeX, open MiKTeX |
| 40 |
Console and open the directories listed under `Settings > Directories` with |
| 41 |
`Purpose` set to `Install`. Doesn't it exist? Append a `tex` or `latex` |
| 42 |
directory to the path. |
| 43 |
|
| 44 |
**Adding the package** |
| 45 |
|
| 46 |
Add a new directory called `highlightlatex` and place the `.sty` file in it. |
| 47 |
Then refresh the file name database: |
| 48 |
* For MiKTeX on Windows open MiKTeX Console and execute `Tasks > Refresh file |
| 49 |
name database`. |
| 50 |
* For TeX Live on Linux or Mac, execute in a terminal `sudo texhash`. |
| 51 |
4. Add `\usepackage{highlightlatex}` to your `.tex` file. |
| 52 |
5. For usage of this package, consult the demo files, or read the |
| 53 |
documentation below. |
| 54 |
|
| 55 |
## Getting started |
| 56 |
|
| 57 |
After having added the package, you can add LaTeX in two ways: |
| 58 |
* Inline style: |
| 59 |
|
| 60 |
Your file begins with a line of the form \hll|\documentclass[]{}|. The |
| 61 |
square brackets ... |
| 62 |
|
| 63 |
The first non-space character following `\hll` is delimits the argument to |
| 64 |
this command. |
| 65 |
|
| 66 |
* Block style: |
| 67 |
|
| 68 |
Your basic document now looks like |
| 69 |
\begin{highlightblock}[gobble=2] |
| 70 |
\documentclass[a4paper]{article} |
| 71 |
\begin{document} |
| 72 |
Hello world! |
| 73 |
\end{document} |
| 74 |
\end{highlightblock} |
| 75 |
|
| 76 |
To prevent indentation of our `highlightblock` (here one tab), to be shown as |
| 77 |
part of the code, the `gobble` parameters strips them off. Play around with |
| 78 |
it until everything looks right. I recommend to set this value globally using |
| 79 |
`\def\defaultgobble{2}`. You can still override that, if necessary. |
| 80 |
|
| 81 |
There are situations where width of the block could run out of the page. For |
| 82 |
example, when using beamer and storing a block as described in the section |
| 83 |
'Fragile breaking situations', the normal full-width of a slide is assumed. |
| 84 |
If you use multiple columns, set the `linewidth` on the `highlightblock`. |
| 85 |
This can be a fraction of the total slide width available, `0.6\textwidth` |
| 86 |
is 60% of the width, or an absolute value, like `10em`, which seems to equal |
| 87 |
20 characters. |
| 88 |
|
| 89 |
There are more keys you can provide. Check the |
| 90 |
[listings package documentation](https://www.ctan.org/pkg/listings) for |
| 91 |
options available to the `lstlisting`-environment and `lstset` command. |
| 92 |
|
| 93 |
## Adding a command to a highlighting rule |
| 94 |
|
| 95 |
By default, only some LaTeX commands will be highlighted in blue. If there are |
| 96 |
others you need, like `\tableofcontents` and `\figref`, update the highlighting |
| 97 |
rules: |
| 98 |
``` |
| 99 |
\updatehighlight{ |
| 100 |
name = default, |
| 101 |
add = { |
| 102 |
\tableofcontents, \figref |
| 103 |
} |
| 104 |
} |
| 105 |
``` |
| 106 |
|
| 107 |
The changes will affect only code after it. For a use as this example, I |
| 108 |
recommend putting it in your preamble (before the `\begin{document}`). |
| 109 |
|
| 110 |
## Custom highlighting rules |
| 111 |
|
| 112 |
As shown `demo.tex`, you can put any command or keyword you want to highlight on |
| 113 |
in a different color. You do this with |
| 114 |
``` |
| 115 |
\updatehighlight{ |
| 116 |
% A name like 'focus' or 'orange' would be more descriptive, |
| 117 |
% this is just to show you can make up the name yourself. |
| 118 |
name = orangejuice, |
| 119 |
color = orange |
| 120 |
add = { |
| 121 |
\tableofcontents |
| 122 |
} |
| 123 |
} |
| 124 |
``` |
| 125 |
|
| 126 |
You can use the `xcolor` syntax for describing colors as well. If you find the |
| 127 |
orange too bright, you can replace it with `orange!90!black`: 90% orange, |
| 128 |
remaining is black. For more information on color definitions and name, refer to |
| 129 |
[LaTeX/Colors on Wikibooks](https://en.wikibooks.org/wiki/LaTeX/Colors). |
| 130 |
|
| 131 |
--- |
| 132 |
|
| 133 |
The argument to `\updatehighlight` is a key-value list. Keys are processed |
| 134 |
sequentially. For example, use `color` before rather than after the `add`, and |
| 135 |
a key can appear multiple times. Each one will be processed. You can merge any |
| 136 |
two `\updatehighlight` in one. No need to close and reopen `\updatehighlight` |
| 137 |
for each highlighting rule. |
| 138 |
|
| 139 |
You might be tempted to add a blank line for clarity; that means a new paragraph |
| 140 |
too LaTeX, don't do it. Instead, just put a line with only a `%` sign. Spacing |
| 141 |
within the argument is often irrelevant. If you need a comma in the value, |
| 142 |
surround your value with braces. |
| 143 |
|
| 144 |
The possible keys are: |
| 145 |
* **name**: Create or modify a named rule. This key is optional. |
| 146 |
|
| 147 |
The default keys are `default`, which includes a bunch of basic commands, |
| 148 |
and has by default a dark blue color, and `structure`, which consists of |
| 149 |
`\begin` and `\end` and prints them in light blue. |
| 150 |
|
| 151 |
_Deprecated alias: label_ |
| 152 |
|
| 153 |
* **classoffset**: Set the `listings` classoffset manually. Try to avoid this. |
| 154 |
Use **name** to refer to existing rules instead. |
| 155 |
|
| 156 |
* **add**: Add a commands (`\mycommand`) oor keywords (`Hi!`) to the current |
| 157 |
rule. The value can contain multiple values by opening braces, and comma |
| 158 |
separating values within them. |
| 159 |
|
| 160 |
_Deprecated aliases: macros (only for commands), commands (only for commands), |
| 161 |
and keywords (only for keywords)_ |
| 162 |
|
| 163 |
* **remove**: Remove a commands or keywords from the current rule. The value can |
| 164 |
contain multiple values by opening braces, and comma separating values within |
| 165 |
them. |
| 166 |
|
| 167 |
* **clear**: Remove all commands and keywords from the current rule. Use without |
| 168 |
value, for example |
| 169 |
|
| 170 |
\updatehighlight{ |
| 171 |
name = default, |
| 172 |
clear |
| 173 |
} |
| 174 |
|
| 175 |
* **color**: Specify a color for the rule. Equivalent to specifying **style** |
| 176 |
instead, with value `\color{value}` where `value` is the value for the |
| 177 |
**color** key. So `color=red` and `style=\color{red}` are equivalent. |
| 178 |
|
| 179 |
* **style**: Specify a style for the rule. A rule can only have one style. If |
| 180 |
you specify a style after `add`, `remove`, `keywords` (deprecated), `macros` |
| 181 |
(deprecated) or `commands` (deprecated), this starts a new (unnamed) rule. |
| 182 |
In practice, the only style which will probably work for you is only a color, |
| 183 |
so just use it instead. But hey, you have the option if you want to. :) |
| 184 |
|
| 185 |
## Global settings |
| 186 |
There are some global parameters involved in the appearance: |
| 187 |
``` |
| 188 |
\colorlet{curlyBrackets}{red!50!blue} |
| 189 |
\colorlet{squareBrackets}{blue!50!white} |
| 190 |
\colorlet{codeBackground}{gray!10!white} |
| 191 |
\colorlet{comment}{green!40!black} |
| 192 |
\def\defaultgobble{0} |
| 193 |
``` |
| 194 |
|
| 195 |
Each line can be set independent of eachother, and each shows its default value. |
| 196 |
|
| 197 |
There are package options you can use as well: |
| 198 |
* **frame** (default `lines`): specify the frame you want around code. My |
| 199 |
favorites are `lines` and `none`. Check the |
| 200 |
[listings package documentation](https://www.ctan.org/pkg/listings) for all |
| 201 |
possibilities. |
| 202 |
|
| 203 |
* **noframe** (use without value): equivalent to `frame=none`. |
| 204 |
|
| 205 |
* **styleanywhere** (use without value): override the default behavior that |
| 206 |
`style` starts a new style after commands like `add` and `remove`. |
| 207 |
|
| 208 |
## Fragile breaking situations (like beamer frames) |
| 209 |
|
| 210 |
When passing command arguments around, or storing environment content, LaTeX |
| 211 |
interprets all characters. This includes seeing `\maketitle` in `\hll|\maketitle|` |
| 212 |
as a real command. To prevent this behavior, everything from `\verb`, to the |
| 213 |
`verbatim`-environment, to the `listings` package the highlight LaTeX package |
| 214 |
uses temporarily changes the interpretation of characters that are still to be |
| 215 |
read. The blackslash before maketitle in `\hll|\maketitle|` will be read as |
| 216 |
`just text' (a _letter_ technically). |
| 217 |
|
| 218 |
When content has already been interpreted, like the `frame`-environment in |
| 219 |
`beamer` does, this trick can't be done anymore. Instead, you either need to |
| 220 |
_escape_ code, or _pre-process_ the code outside a fragile breaking situation. |
| 221 |
|
| 222 |
Escaping is done by preceding the special character with a backslash. For |
| 223 |
example, `\hll|\documentclass[]{}|` becomes `\hll|\\documentclass[]\{\}|`. |
| 224 |
|
| 225 |
For large code blocks, this is undesirable. Therefore, the package provides for |
| 226 |
a companion to the `highlightblock`-environment: surround it with a `saveblock` |
| 227 |
environment which takes a single argument: a name to assign it. We use it to |
| 228 |
refer to it later. For example: |
| 229 |
```latex |
| 230 |
\begin{saveblock}{basicfigure} |
| 231 |
\begin{highlightblock}[linewidth=0.6\textwidth] |
| 232 |
\begin{figure} |
| 233 |
\includegraphics |
| 234 |
[width=0.9\linewidth] |
| 235 |
{myPlot.pdf} |
| 236 |
|
| 237 |
\caption{My plot} |
| 238 |
\label{fig:myplot} |
| 239 |
\end{figure} |
| 240 |
\end{highlightblock} |
| 241 |
\end{saveblock} |
| 242 |
``` |
| 243 |
|
| 244 |
Do this outside a fragile breaking situation. (For the `frame`-environment |
| 245 |
example, that means just before the `frame` for example.) Then, where you want |
| 246 |
to use it, use `\useblock{basicfigure}`. There is also a variant |
| 247 |
`\consumeblock{basicfigure}`. If you save many blocks, these will all remain |
| 248 |
loaded in memory till your PDF has fully generated. The `\consumeblock` works |
| 249 |
like `\useblock`, except the saved block is deleted from memory after its use. |
| 250 |
Note this can also result in unexpected behavior, for example animations in a |
| 251 |
beamer frame might need the code line to be executed multiple times. Use |
| 252 |
`\useblock` when you can't make the guarantee this is the last use. |
| 253 |
|
| 254 |
For this special, but very important, situation, I've created a separate demo, |
| 255 |
which you find at `deamerdemo/deamerdemo.tex`. |
| 256 |
|
| 257 |
## Adding extra space |
| 258 |
|
| 259 |
By default, highlight-latex follows an approach where it minimizes spacing. |
| 260 |
This gives you full control over how tight or spacious your document looks. |
| 261 |
Just use commands like `\medskip` to add extra spacing. The package doesn't |
| 262 |
currently include an option to have that done everywhere automatically. |
| 263 |
|
| 264 |
## License |
| 265 |
|
| 266 |
The package is available under MIT License. See LICENSE.txt |
| 267 |
|