Code Highlight with minted in LaTeX

“minted” is good choice for code highlighting in LaTeX.

If you write your LaTeX in Overleaf, just refer to Overleaf: Code Highlighting with minted

If you want to use minted locally, you can continue reading.

Install minted

Use TeX Live package manager to install minted, then use pip to install pygments that minted requires.

sudo tlmgr install minted
pip install pygments

Config minted

Package minted: You must invoke LaTeX with the -shell-escape flag.

minted needs -shell-escape flag to be enabled, add “-shell-escape” in args of the tool you use in the “latex-workshop.latex.tools” in “settings.json”

or simply add that flag in terminal if you directly use command line to build LaTeX project

Troubleshooting

  1. If you encounter warning that suggest you to upgrade you mactex (livetex) version, you can simply use brew upgrade mactex to upgrade it. Or you can refer to the official instruction (https://tug.org/texlive/upgrade.html).
  2. Warning: tlgmr itself may also need update sudo tlmgr update --self