% \iffalse meta-comment % % Copyright (C) 2013-2015 by Geoffrey M. Poore % Copyright (C) 2010-2011 by Konrad Rudolph % --------------------------------------------------------------------------- % This work may be distributed and/or modified under the % conditions of the LaTeX Project Public License, either version 1.3 % of this license or (at your option) any later version. % The latest version of this license is in % http://www.latex-project.org/lppl.txt % and version 1.3 or later is part of all distributions of LaTeX % version 2005/12/01 or later. % % Additionally, the project may be distributed under the terms of the 3-Clause % ("New") BSD license: http://opensource.org/licenses/BSD-3-Clause. % % This work has the LPPL maintenance status `maintained'. % % The Current Maintainer of this work is Geoffrey M. Poore. % % This work consists of the files minted.dtx and minted.ins % and the derived file minted.sty. % % % \fi % % \iffalse %<*driver> \ProvidesFile{minted.dtx} % %\NeedsTeXFormat{LaTeX2e} %\ProvidesPackage{minted} %<*package> [2015/09/09 v2.1 Yet another Pygments shim for LaTeX] % %<*driver> \documentclass{ltxdoc} \EnableCrossrefs \CodelineIndex %\DisableCrossrefs %\RecordChanges %\OnlyDescription \usepackage{fixltx2e} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} \usepackage{lmodern} \usepackage{lstdoc} \usepackage{xcolor} \usepackage{upquote} \usepackage[cache, langlinenos]{minted} % Need to set the style here so that it is defined and brought in here. % If the style is set later, then docstrip interferes with any comments in % the style definition so that they appear as literal text in the document. \setminted{style=default} \usepackage{courier} % Useful monospace font (= has \bfseries). \usepackage{textcomp} \usepackage{microtype} \usepackage{environ} \usepackage{graphicx} \usepackage{dingbat} \usepackage{multicol} \usepackage{hyperref} \makeatletter \newcommand{\changestext}{} \NewEnviron{changelog}[2]{% \g@addto@macro\changestext{\item[#1] (#2) \begin{itemize}}% \expandafter\g@addto@macro\expandafter\changestext\expandafter{\BODY}% \g@addto@macro\changestext{\end{itemize}}% } \newcommand{\PrintChangelog}{% \addcontentsline{toc}{section}{Version History} \section*{Version History}% \label{sec:version-history} \begin{description}% \changestext \end{description}% } \def\MacroFont{% \fontencoding\encodingdefault% \fontfamily\ttdefault% \fontseries\mddefault% \fontshape\updefault% \small} % FIXME This is incredibly hacky and overrides size commands around tt-text. % But I don't get the font inside the main body to change otherwise. :-( \let\mintedttold\ttfamily \def\ttfamily{\mintedttold\fontsize{9}{9}\selectfont} % \definecolor{minted@mint}{HTML}{0B610B} \definecolor{minted@samplebg}{HTML}{F0F0E0} \colorlet{minted@linkcolor}{minted@mint} % \def\PrintDescribeMacro#1{\strut \MacroFont\textcolor{minted@linkcolor}{\string #1\ }} \let\PrintDescribeEnv\PrintDescribeMacro \let\PrintMacroName\PrintDescribeMacro \let\PrintEnvName\PrintDescribeEnv \def\theCodelineNo{\textcolor{minted@linkcolor}{\sffamily\footnotesize\oldstylenums{\arabic{CodelineNo}}}} % \hypersetup{ pdftitle=The minted package: Highlighted source code in LaTeX, pdfauthor=Geoffrey M. Poore, pdfsubject={Minted LaTeX package manual}, allcolors=minted@linkcolor, } % % Shamelessly stolen from http://blog.karssen.org/2009/11/15/a-latex-example-environment/ \newenvironment{example} {\VerbatimEnvironment \begin{VerbatimOut}[gobble=3]{example.out}} {\end{VerbatimOut}% \vspace{1ex}% \setlength{\parindent}{0pt}% \fbox{\begin{minipage}{0.5\linewidth}% \inputminted[resetmargins]{latex}{example.out}% \end{minipage}% \hspace{0.05\linewidth}% \begin{minipage}{0.4\linewidth}% \input{example.out}% \end{minipage}% \vspace{1ex}}} \newenvironment{longexample} {\VerbatimEnvironment \begin{VerbatimOut}[gobble=3]{example.out}} {\end{VerbatimOut}% \vspace{1ex}% \setlength{\parindent}{0pt}% \fbox{\begin{minipage}{0.95\linewidth}% \inputminted[resetmargins]{latex}{example.out}% ~\hrulefill~ \input{example.out}% \end{minipage}% \vspace{1ex}}} \def\minted@printopt#1(#2) (#3){% \leavevmode% \marginpar{\raggedleft\texttt{\textcolor{minted@linkcolor}{#1}}\ }% \textsf{(#2)}\hfill(default: #3)\\} \newenvironment{optionlist}{% \par% \newcommand*\mintednext{}% \renewcommand*\item[1][]{% \mintednext% \renewcommand*\mintednext{\par}% \minted@printopt##1% \ignorespaces} \DeleteShortVerb{\|}% \MakeShortVerb{\+}}{% \DeleteShortVerb{\+}% \MakeShortVerb{\|}% \par} \edef\hashchar{\string#} \makeatother \newcommand{\hide}[1]{} \setlength{\parindent}{0pt} \addtolength{\parskip}{0.5\baselineskip} \begin{document} \DocInput{minted.dtx} \PrintIndex \end{document} % % \fi % % \CheckSum{3283} % % \CharacterTable % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z % Digits \0\1\2\3\4\5\6\7\8\9 % Exclamation \! Double quote \" Hash (number) \# % Dollar \$ Percent \% Ampersand \& % Acute accent \' Left paren \( Right paren \) % Asterisk \* Plus \+ Comma \, % Minus \- Point \. Solidus \/ % Colon \: Semicolon \; Less than \< % Equals \= Greater than \> Question mark \? % Commercial at \@ Left bracket \[ Backslash \\ % Right bracket \] Circumflex \^ Underscore \_ % Grave accent \` Left brace \{ Vertical bar \| % Right brace \} Tilde \~} % % % % \begin{changelog}{v2.1}{2015/09/09} % \item Changing the highlighting style now no longer involves re-highlighing code. Style may be changed with almost no overhead. % \item Improved control of automatic line breaks. New option \texttt{breakanywhere} allows line breaks anywhere when \texttt{breaklines=true}. The pre-break and post-break symbols for these types of breaks may be set with \texttt{breakanywheresymbolpre} and \texttt{breakanywheresymbolpost} (\#79). New option \texttt{breakafter} allows specifying characters after which line breaks are allowed. Breaks between adjacent, identical characters may be controlled with \texttt{breakaftergroup}. The pre-break and post-break symbols for these types of breaks may be set with \texttt{breakaftersymbolpre} and \texttt{breakaftersymbolpost}. % \item \texttt{breakbytoken} now only breaks lines between tokens that are separated by spaces, matching the documentation. The new option \texttt{breakbytokenanywhere} allows for breaking between tokens that are immediately adjacent. Fixed a bug in \texttt{\string\mintinline} that produced a following linebreak when \texttt{\string\mintinline} was the first thing in a paragraph and \texttt{breakbytoken} was true (\#77). % \item Fixed a bug in draft mode option handling for \texttt{\string\inputminted} (\#75). % \item Fixed a bug with \texttt{\string\MintedPygmentize} when a custom \texttt{pygmentize} was specified and there was no \texttt{pygmentize} on the default path (\#62). % \item Added note to docs on caching large numbers of code blocks under OS~X (\#78). % \item Added discussion of current limitations of \texttt{texcomments} (\#66) and \texttt{escapeinside} (\#70). % \item PGF/Ti\textit{k}Z externalization is automatically detected and supported (\#73). % \item The package is now compatible with \LaTeX\ files whose names contain spaces (\#85). % \end{changelog} % % % \begin{changelog}{v2.0}{2015/01/31} % \item Added the compatibility package \texttt{minted1}, which provides the \pkg{minted} 1.7 code. This may be loaded when 1.7 compatibility is required. This package works with other packages that \texttt{\string\RequirePackage\{minted\}}, so long as it is loaded first. % \item Moved all old \texttt{\string\changes} into \texttt{changelog}. % \end{changelog} % % % \begin{changelog}{Development releases for 2.0}{2014--January 2015} % \item Caching is now on by default. % \item Fixed a bug that prevented compiling under Windows when file names contained commas. % \item Added \texttt{breaksymbolleft}, \texttt{breaksymbolsepleft}, \texttt{breaksymbolindentleft}, \texttt{breaksymbolright}, \texttt{breaksymbolsepright}, and \texttt{breaksymbolindentright} options. \texttt{breaksymbol}, \texttt{breaksymbolsep}, and \texttt{breaksymbolindent} are now aliases for the correspondent \texttt{*left} options. % \item Added \texttt{kpsewhich} package option. This uses \texttt{kpsewhich} to locate the files that are to be highlighted. This provides compatibility with build tools like \texttt{texi2pdf} that function by modifying \texttt{TEXINPUTS} (\#25). % \item Fixed a bug that prevented \texttt{\string\inputminted} from working with \texttt{outputdir}. % \item Added informative error messages when Pygments output is missing. % \item Added \texttt{final} package option (opposite of \texttt{draft}). % \item Renamed the default cache directory to \texttt{\_minted-} (replaced leading period with underscore). The leading period caused the cache directory to be hidden on many systems, which was a potential source of confusion. % \item \texttt{breaklines} and \texttt{breakbytoken} now work with \texttt{\string\mintinline} (\#31). % \item \texttt{bgcolor} may now be set through \texttt{\string\setminted} and \texttt{\string\setmintedinline}. % \item When math is enabled via \texttt{texcomments}, \texttt{mathescape}, or \texttt{escapeinside}, space characters now behave as in normal math by vanishing, instead of appearing as literal spaces. Math need no longer be specially formatted to avoid undesired spaces. % \item In default value of \texttt{\string\listoflistingscaption}, capitalized ``Listings'' so that capitalization is consistent with default values for other lists (figures, tables, algorithms, etc.). % \item Added \texttt{newfloat} package option that creates the \texttt{listing} environment using \texttt{newfloat} rather than \texttt{float}, thus providing better compatibility with the \texttt{caption} package (\#12). % \item Added support for Pygments option \texttt{stripall}. % \item Added \texttt{breakbytoken} option that prevents \texttt{breaklines} from breaking lines within Pygments tokens. % \item \texttt{\string\mintinline} uses a \texttt{\string\colorbox} when \texttt{bgcolor} is set, to give more reasonable behavior (\#57). % \item For PHP, \texttt{\string\mintinline} automatically begins with \texttt{startinline=true} (\#23). % \item Fixed a bug that threw off line numbering in \texttt{minted} when \texttt{langlinenos=false} and \texttt{firstnumber=last}. Fixed a bug in \texttt{\string\mintinline} that threw off subsequent line numbering when \texttt{langlinenos=false} and \texttt{firstnumber=last}. % \item Improved behavior of \texttt{\string\mint} and \texttt{\string\mintinline} in \texttt{draft} mode. % \item The \texttt{\string\mint} command now has the additional capability to take code delimited by paired curly braces \texttt{\{\}}. % \item It is now possible to set options only for \texttt{\string\mintinline} using the new \texttt{\string\setmintedinline} command. Inline options override options specified via \texttt{\string\setminted}. % \item Completely rewrote option handling. \pkg{fancyvrb} options are now handled on the \LaTeX\ side directly, rather than being passed to Pygments and then returned. This makes caching more efficient, since code is no longer rehighlighted just because \pkg{fancyvrb} options changed. % \item Fixed buffer size error caused by using \texttt{cache} with a very large number of files (\#61). % \item Fixed \texttt{autogobble} bug that caused failure under some operating systems. % \item Added support for \texttt{escapeinside} (requires Pygments 2.0+; \#38). % \item Fixed issues with XeTeX and caching (\#40). % \item The \texttt{upquote} package now works correctly with single quotes when using Pygments 1.6+ (\#34). % \item Fixed caching incompatibility with Linux and OS X under xelatex (\#18 and \#42). % \item Fixed \texttt{autogobble} incompatibility with Linux and OS X. % \item \texttt{\string\mintinline} and derived commands are now robust, via \texttt{\string\newrobustcmd} from \texttt{etoolbox}. % \item Unused styles are now cleaned up when caching. % \item Fixed a bug that could interfere with caching (\#24). % \item Added \texttt{draft} package option (\#39). This typesets all code using \texttt{fancyvrb}; Pygments is not used. This trades syntax highlighting for maximum speed in compiling. % \item Added automatic line breaking with \texttt{breaklines} and related options (\#1). % \item Fixed a bug with boolean options that needed a False argument to cooperate with \texttt{\string\setminted} (\#48). % \end{changelog} % % \begin{changelog}{v2.0-alpha3}{2013/12/21} % \item Added \texttt{autogobble} option. This sends code through Python's \texttt{textwrap.dedent()} to remove common leading whitespace. % \item Added package option \texttt{cachedir}. This allows the directory in which cached content is saved to be specified. % \item Added package option \texttt{outputdir}. This allows an output directory for temporary files to be specified, so that the package can work with LaTeX's \texttt{-output-directory} command-line option. % \item The \texttt{kvoptions} package is now required. It is needed to process key-value package options, such as the new \texttt{cachedir} option. % \item Many small improvements, including better handling of paths under Windows and improved key system. % \end{changelog} % % \begin{changelog}{v2.0-alpha2}{2013/08/21} % \item \texttt{\string\DeleteFile} now only deletes files if they do indeed exist. This eliminates warning messages due to missing files. % \item Fixed a bug in the definition of \texttt{\string\DeleteFile} for non-Windows systems. % \item Added support for Pygments option \texttt{stripnl}. % \item Settings macros that were previously defined globally are now defined locally, so that \texttt{\string\setminted} may be confined by \texttt{\string\begingroup...\string\endgroup} as expected. % \item Macro definitions for a given style are now loaded only once per document, rather than once per command/environment. This works even without caching. % \item A custom script/executable may now be substituted for \texttt{pygmentize} by redefining \texttt{\string\MintedPygmentize}. % \end{changelog} % % % \begin{changelog}{v2.0alpha}{2013/07/30} % \item Added the package option \texttt{cache}. This significantly increases compilation speed by caching old output. For example, compiling the documentation is around 5x faster. % \item New inline command \texttt{\string\mintinline}. Custom versions can be created via \texttt{\string\newmintinline}. The command works inside other commands (for example, footnotes) in most situations, so long as the percent and hash characters are avoided. % \item The new \texttt{\string\setminted} command allows options to be specified at the document and language levels. % \item All extended characters (Unicode, etc.) supported by \texttt{inputenc} now work under the pdfTeX engine. This involved using \texttt{\string\detokenize} on everything prior to saving. % \item New package option \texttt{langlinenos} allows line numbering to pick up where it left off for a given language when \texttt{firstnumber=last}. % \item New options, including \texttt{style}, \texttt{encoding}, \texttt{outencoding}, \texttt{codetagify}, \texttt{keywordcase}, \texttt{texcomments} (same as \texttt{texcl}), \texttt{python3} (for the \texttt{PythonConsoleLexer}), and \texttt{numbers}. % \item \texttt{\string\usemintedstyle} now takes an optional argument to specify the style for a particular language, and works anywhere in the document. % \item \texttt{xcolor} is only loaded if \texttt{color} isn't, preventing potential package clashes. % \end{changelog} % % % \begin{changelog}{1.7}{2011/09/17} % \item Options for float placement added [2011/09/12] % \item Fixed \texttt{tabsize} option [2011/08/30] % \item More robust detection of the \texttt{-shell-escape} option [2011/01/21] % \item Added the \texttt{label} option [2011/01/04] % \item Installation instructions added [2010/03/16] % \item Minimal working example added [2010/03/16] % \item Added PHP-specific options [2010/03/14] % \item Removed unportable flag from Unix shell command [2010/02/16] % \end{changelog} % % % \begin{changelog}{1.6}{2010/01/31} % \item Added font-related options [2010/01/27] % \item Windows support added [2010/01/27] % \item Added command shortcuts [2010/01/22] % \item Simpler versioning scheme [2010/01/22] % \end{changelog} % % % \begin{changelog}{0.1.5}{2010/01/13} % \item Added \texttt{fillcolor} option [2010/01/10] % \item Added float support [2010/01/10] % \item Fixed \texttt{firstnumber} option [2010/01/10] % \item Removed \texttt{caption} option [2010/01/10] % \end{changelog} % % % \begin{changelog}{0.0.4}{2010/01/08} % \item Initial version [2010/01/08] % \end{changelog} % % % %\DoNotIndex{\newcommand,\newenvironment} % %\DoNotIndex{\#,\$,\%,\&,\@,\\,\{,\},\^,\_,\~,\ } % %\DoNotIndex{\@ne} % %\DoNotIndex{\advance,\begingroup,\catcode,\closein} % %\DoNotIndex{\closeout,\day,\def,\edef,\else,\empty,\endgroup} % %\DoNotIndex{\begin,\end,\bgroup,\egroup} % %\DoNotIndex{\@namedef,\@nameuse,=,\csname,\endcsname} % % % \GetFileInfo{minted.sty} % % \newcommand\pkg[1]{\textsf{#1}} % \newcommand\app[1]{\textsf{#1}} % % \title{The \textcolor{minted@mint}{\pkg{minted}} package:\\Highlighted source code in \LaTeX} % \author{Geoffrey M.\ Poore \\ \url{gpoore@gmail.com} \\ \href{https://github.com/gpoore/minted}{\texttt{github.com/gpoore/minted}} \\ ~\\ Originally created and maintained (2009--2013) by \\ Konrad Rudolph} % \date{\fileversion~from \filedate} % % \maketitle % % \begin{abstract} % \noindent\pkg{minted} is a package that facilitates expressive syntax highlighting % using the powerful \app{Pygments} library. The package also provides options to % customize the highlighted source code output. % \end{abstract} % % \vspace{2in} % % \subsection*{License} % \href{http://www.latex-project.org/lppl.txt}{LaTeX Project Public License (LPPL)} version 1.3. % % Additionally, the project may be distributed under the terms of the 3-Clause % (``New'') BSD license: \url{http://opensource.org/licenses/BSD-3-Clause}. % % \pagebreak % % \tableofcontents % % \fvset{ % codes={\catcode`\%=9}, ^^A Ignore initial |%| % numbersep=5pt, % fontsize=\small % } % \setlength{\fboxsep}{1ex} % % \mbox{}\newpage % \section{Introduction} % % % % \pkg{minted} is a package that allows formatting source code in \LaTeX. % For example: % \begin{VerbatimOut}[gobble=1]{minted.doc.out} % \begin{minted}{} % % \end{minted} % \end{VerbatimOut} % \inputminted[gobble=2,frame=lines]{latex}{minted.doc.out} % % will highlight a piece of code in a chosen language. % The appearance can be customized with a number of options and color schemes. % % Unlike some other packages, most notably \pkg{listings}, \pkg{minted} requires % the installation of additional software, \app{Pygments}. % This may seem like a disadvantage, but there are also significant advantages. % % \app{Pygments} provides superior syntax highlighting compared to conventional packages. % For example, \pkg{listings} basically only highlights strings, comments and keywords. % \app{Pygments}, on the other hand, can be completely customized to highlight any kind of token the % source language might support. % This might include special formatting sequences inside strings, numbers, different kinds of % identifiers and exotic constructs such as HTML tags. % % Some languages make this especially desirable. % Consider the following Ruby code as an extreme, but at the same time typical, example: % % \begin{minted}[gobble=3]{ruby} % class Foo % def init % pi = Math::PI % @var = "Pi is approx. #{pi}" % end % end % \end{minted} % % Here we have four different colors for identifiers (five, if you count keywords) and escapes from % inside strings, none of which pose a problem for \app{Pygments}. % % Additionally, installing \app{Pygments} is actually incredibly easy (see the next section). % % % \section{Installation} % % \subsection{Prerequisites} % % \app{Pygments} is written in Python, so make sure that you have Python 2.6 or later installed on your system. This may be easily checked from the command line: % % \begin{Verbatim}[gobble=3,commandchars=\\\{\}] % \$ python --version % Python 2.7.5 % \end{Verbatim} % % If you don't have Python installed, you can download it from the \href{http://www.python.org/download/}{Python website} or % use your operating system's package manager. % % Some Python distributions include \pkg{Pygments} (see some of the options under ``Alternative Implementations'' on the Python site). Otherwise, you will need to install \pkg{Pygments} manually. This may be done by installing \href{http://pypi.python.org/pypi/setuptools}{\app{setuptools}}, which facilitates the distribution of Python applications. You can then install \app{Pygments} using the following command: % \begin{Verbatim}[gobble=3,commandchars=\\\{\}] % \$ sudo easy_install Pygments % \end{Verbatim} % Under Windows, you will not need the |sudo|, but may need to run the command prompt as administrator. \pkg{Pygments} may also be installed with |pip|: % \begin{Verbatim}[gobble=3,commandchars=\\\{\}] % \$ pip install Pygments % \end{Verbatim} % % If you already have \app{Pygments} installed, be aware that the latest version is recommended (at least 1.4 or later). Some features, such as |escapeinside|, will only work with 2.0+. \pkg{minted} may work with versions as early as 1.2, but there are no guarantees. % % % \subsection{Required packages} % % \pkg{minted} requires that the following packages be available and reasonably up to date on your system. All of these ship with recent \TeX\ distributions. % % \begin{multicols}{3} % \begingroup % \setlength\parskip{0pt} % \setlength\topsep{0pt} % \begin{list}{\textrm{\labelitemi}}{\ttfamily} % \item keyval % \item kvoptions % \item fancyvrb % \item float % \item ifthen % \item calc % \item ifplatform % \item pdftexcmds % \item etoolbox % \item xstring % \item xcolor % \item lineno % \end{list} % \endgroup % \end{multicols} % % % \subsection{Installing \pkg{minted}} % \label{sec:installing:installing} % % You can probably install \pkg{minted} with your \TeX\ distribution's package manager. Otherwise, or if you want the absolute latest version, you can install it manually by following the directions below. % % You may download |minted.sty| from the % \href{https://github.com/gpoore/minted}{project's homepage}. We have to install the file so that \TeX\ is able to find it. % In order to do that, please refer to the % \href{http://www.tex.ac.uk/cgi-bin/texfaq2html?label=inst-wlcf}{\TeX{} FAQ}. % If you just want to experiment with the latest version, you could locate your current |minted.sty| in your \TeX\ installation and replace it with the latest version. Or you could just put the latest |minted.sty| in the same directory as the file you wish to use it with. % % % \section{Transitioning to version 2} % % Transitioning from \pkg{minted} 1.7 to 2.0+ should require no changes in almost all cases. Version 2 provides the same interface and all of the same features. % % In cases when custom code was used to hook into the \pkg{minted} internals, it may still be desirable to use the old \pkg{minted} 1.7. For those cases, the new package \pkg{minted1} is provided. Simply load this before any other package attempts to load \pkg{minted}, and you will have the code from 1.7. % % A brief summary of new features in version 2.0 is provided below. More detail is available in the \hyperref[sec:version-history]{Version History}. % \begin{itemize} % \item New inline command |\mintinline|. % \item Support for caching highlighted code with new package option |cache|. This drastically reduces package overhead. Caching is on by default. A cache directory called |_minted-|\meta{document~name} will be created in the document root directory. This may be modified with the |cachedir| package option. % \item Automatic line breaking for all commands and environments with new option |breaklines|. Many additional options for customizing line breaking. % \item Support for Unicode under the pdfTeX engine. % \item Set document-wide options using |\setminted{|\meta{opts}|}|. Set language-specific options using |\setminted[|\meta{lang}|]{|\meta{opts}|}|. Similarly, set inline-specific options using |\setmintedinline|. % \item Package option |langlinenos|: do line numbering by language. % \item Many new options, including |encoding|, |autogobble|, and |escapeinside| (requires Pygments 2.0+). % \item New package option |outputdir| provides compatibility with command-line options |-output-directory| and |-aux-directory|. % \item New package option |draft| disables Python use to give maximum performance. % \item |\mint| can now take code delimited by matched curly braces |{}|. % \end{itemize} % % % \section{Basic usage} % % \subsection{Preliminary} % \label{sec:basic:preliminary} % % Since \pkg{minted} makes calls to the outside world (that is, \app{Pygments}), you need to tell the % \LaTeX{} processor about this by passing it the |-shell-escape| option or it won't allow such calls. % In effect, instead of calling the processor like this: % \begin{Verbatim}[gobble=3,commandchars=\\\{\}] % \$ latex input % \end{Verbatim} % % you need to call it like this: % % \begin{Verbatim}[gobble=3,commandchars=\\\{\}] % \$ latex -shell-escape input % \end{Verbatim} % % The same holds for other processors, such as |pdflatex| or |xelatex|. % % You should be aware that using |-shell-escape| allows \LaTeX\ to run potentially arbitrary commands on your system. It is probably best to use |-shell-escape| only when you need it, and to use it only with documents from trusted sources. % % % \subsubsection*{Working with OS X} % % If you are using \pkg{minted} with some versions/configurations of OS X, and are using caching with a large number of code blocks ($>256$), you may receive an error like %\begin{Verbatim} %OSError: [Errno 24] Too many open files: %\end{Verbatim} % This is due to the way files are handled by the operating system, combined with the way that caching works. To resolve this, you may use the OS X commands |launchctl limit maxfiles| or |ulimit -n| to increase the number of files that may be used. % % % % \subsection{A minimal complete example} % % The following file |minimal.tex| shows the basic usage of \pkg{minted}. % % \begin{VerbatimOut}[gobble=1]{minted.doc.out} % \documentclass{article} % % \usepackage{minted} % % \begin{document} % \begin{minted}{c} % int main() { % printf("hello, world"); % return 0; % } % \end{minted} % \end{document} % \end{VerbatimOut} % \inputminted[gobble=2,frame=lines]{latex}{minted.doc.out} % % By compiling the source file like this: % % \begin{Verbatim}[gobble=3,commandchars=\\\{\}] % \$ pdflatex -shell-escape minimal % \end{Verbatim} % % we end up with the following output in |minimal.pdf|: % % \hfill % \colorbox{minted@samplebg}{\begin{minipage}{0.6\textwidth} % \inputminted[firstline=7,lastline=10]{c}{minted.doc.out} % \end{minipage}} % \hfill\hfill % % % \subsection{Formatting source code} % % \DescribeEnv{minted} % Using \pkg{minted} is straightforward. For example, to highlight some Python source code we might use % the following code snippet (result on the right): % % \begin{example} % \begin{minted}{python} % def boring(args = None): % pass % \end{minted} % \end{example} % % Optionally, the environment accepts a number of options in |key=value| notation, which are described % in more detail below. % % % \DescribeMacro{\mint} % For a single line of source code, you can alternatively use a shorthand notation: % % \begin{example} % \mint{python}|import this| % \end{example} % % This typesets a single line of code using a command rather than an environment, so it saves a little typing, but its output is equivalent to that of the |minted| environment. % % The code is delimited by a pair of identical characters, similar to how |\verb| works. The complete syntax is \cmd\mint\oarg{options}\marg{language}\meta{delim}\meta{code}\meta{delim}, % where the code delimiter can be almost any punctuation character. % The \meta{code} may also be delimited with matched curly braces |{}|, so long as \meta{code} itself does not contain unmatched curly braces. % Again, this command supports a number of options described below. % % Note that the |\mint| command \textbf{is not for inline use}. Rather, it is a shortcut for |minted| when only a single line of code is present. The |\mintinline| command is provided for inline use. % % % \DescribeMacro{\mintinline} % Code can be typeset inline: % % \begin{example} % X\mintinline{python}{print(x**2)}X % \end{example} % % The syntax is \cmd\mintinline\oarg{options}\marg{language}\meta{delim}\meta{code}\meta{delim}. The delimiters can be a pair of characters, as for \cmd\mint. They can also be a matched pair of curly braces, |{}|. % % The command has been carefully crafted so that in most cases it will function correctly when used inside other commands.\footnote{For example, \mintinline{latex}{\mintinline} works in footnotes! The main exception is when the code contains the percent \texttt{\%} or hash \texttt{\#} characters, or unmatched curly braces.} % % \DescribeMacro{\inputminted} % Finally, there's the |\inputminted| command to read and format whole files. % Its syntax is \cmd\inputminted\oarg{options}\marg{language}\marg{filename}. % % % \subsection{Using different styles} % % \DescribeMacro{\usemintedstyle} % Instead of using the default style you may choose another stylesheet provided by \app{Pygments}. This may be done via the following: % % \mint[frame=lines]{latex}/\usemintedstyle{name}/ % % The full syntax is \cmd\usemintedstyle\oarg{language}\marg{style}. The style may be set for the document as a whole (no language specified), or only for a particular language. Note that the style may also be set via \cmd\setminted\ and via the optional argument for each command and environment.\footnote{Version 2.0 added the optional language argument and removed the restriction that the command be used in the preamble.} % % To get a list of all available stylesheets, see the online demo at the \href{http://pygments.org/demo/}{Pygments website} or execute the following command on the command line: % % \begin{Verbatim}[gobble=3,commandchars=\\\{\}] % \$ pygmentize -L styles % \end{Verbatim} % % % Creating your own styles is also easy. Just follow the instructions provided on the % \href{http://pygments.org/docs/styles/#creating-own-styles}{\pkg{Pygments} website}. % % % \subsection{Supported languages} % % \app{Pygments} supports over 300 different programming languages, template languages, and other markup languages. To see an exhaustive list of the currently supported languages, use the command % % \begin{Verbatim}[gobble=3,commandchars=\\\{\}] % \$ pygmentize -L lexers % \end{Verbatim} % % % \section{Floating listings}\label{sec:float} % % \DescribeEnv{listing} % \pkg{minted} provides the |listing| environment to wrap around a source code block. This puts the code into a floating box. You can also provide a |\caption| and a |\label| for such a listing in the usual way (that is, % as for the |table| and |figure| environments): % \begin{VerbatimOut}[gobble=1]{minted.doc.out} % \begin{listing}[H] % \mint{cl}/(car (cons 1 '(2)))/ % \caption{Example of a listing.} % \label{lst:example} % \end{listing} % % Listing \ref{lst:example} contains an example of a listing. % \end{VerbatimOut} % \inputminted[gobble=2,frame=lines]{latex}{minted.doc.out} % % will yield: % % \hfill % \colorbox{minted@samplebg}{\begin{minipage}{0.6\textwidth} % \input{minted.doc.out} % \end{minipage}} % \hfill\hfill % % \DescribeMacro{\listoflistings} % The |\listoflistings| macro will insert a list of all (floated) listings in the document: % % \begin{example} % \listoflistings % \end{example} % % \subsection*{Customizing the \texttt{listing} environment} % By default, the |listing| environment is created using the \pkg{float} package. In that case, the |\listingscaption| and |\listoflistingscaption| macros described below may be used to customize the caption and list of listings. If \pkg{minted} is loaded with the |newfloat| option, then the |listing| environment will be created with the more powerful \href{http://www.ctan.org/pkg/newfloat}{\pkg{newfloat}} package instead. \pkg{newfloat} is part of \href{http://www.ctan.org/pkg/caption}{\pkg{caption}}, which provides many options for customizing captions. % % When \pkg{newfloat} is used to create the |listing| environment, customization should be achieved using \pkg{newfloat}'s |\SetupFloatingEnvironment| command. For example, the string ``Listing'' in the caption could be changed to ``Program code'' using %\begin{verbatim} %\SetupFloatingEnvironment{listing}{name=Program code} %\end{verbatim} % And ``List of Listings'' could be changed to ``List of Program Code'' with %\begin{verbatim} %\SetupFloatingEnvironment{listing}{listname=List of Program Code} %\end{verbatim} % Refer to the \pkg{newfloat} and \pkg{caption} documentation for additional information. % % \DescribeMacro{\listingscaption} % (Only applies when package option |newfloat| is not used.) The string ``Listing'' in a listing's caption can be changed. % To do this, simply redefine the macro |\listingscaption|, for example: % % \mint[frame=lines]{latex}/\renewcommand{\listingscaption}{Program code}/ % % \DescribeMacro{\listoflistingscaption} % (Only applies when package option |newfloat| is not used.) Likewise, the caption of the listings list, ``List of Listings,'' can be changed by redefining % |\listoflistingscaption|: % % \mint[frame=lines]{latex}/\renewcommand{\listoflistingscaption}{List of Program Code}/ % % % \section{Options} % % % % \subsection{Package options} % % \DescribeMacro{chapter} % To control how \LaTeX{} counts the |listing| floats, you can pass either the % |section| or |chapter| option when loading the \pkg{minted} package. % For example, the following will cause listings to be counted by chapter: % % \mint[frame=lines]{latex}/\usepackage[chapter]{minted}/ % % % \DescribeMacro{cache=\meta{boolean} (default: true)} % \pkg{minted} works by saving code to a temporary file, highlighting the code via \app{Pygments} and saving the output to another temporary file, and inputting the output into the \LaTeX\ document. This process can become quite slow if there are several chunks of code to highlight. To avoid this, the package provides a |cache| option. This is on by default. % % The |cache| option creates a directory |_minted-|\meta{jobname} in the document's root directory (this may be customized with the |cachedir| option).\footnote{The directory is actually named using a ``sanitized'' copy of \meta{jobname}, in which spaces and asterisks have been replaced by underscores, and double quotation marks have been stripped. If the file name contains spaces, \texttt{\string\jobname} will contain a quote-wrapped name, except under older versions of MiKTeX which used the name with spaces replaced by asterisks. Using a ``sanitized'' \meta{jobname} is simpler than accomodating the various escaping conventions.} Files of highlighted code are stored in this directory, so that the code will not have to be highlighted again in the future. In most cases, caching will significantly speed up document compilation. % % Cached files that are no longer in use are automatically deleted.\footnote{This depends on the main auxiliary file not being deleted or becoming corrupted. If that happens, you could simply delete the cache directory and start over.} % % % \DescribeMacro{cachedir=\meta{directory} (def:~\_minted-\meta{jobname})} % This allows the directory in which cached files are stored to be specified. Paths should use forward spaces, even under Windows. % % Note that this directory is relative to the |outputdir|, if an |outputdir| is specified. % % % \DescribeMacro{draft=\meta{boolean} (default: false)} % This uses \pkg{fancyvrb} alone for all typesetting; \app{Pygments} is not used. This trades syntax highlighting and some other \app{minted} features for faster compiling. Performance should be essentially the same as using \pkg{fancyvrb} directly; no external temporary files are used. Note that if you are not changing much code between compiles, the difference in performance between caching and draft mode may be minimal. Also note that |draft| settings are typically inherited from the document class. % % Draft mode does not support |autogobble|. Regular |gobble|, |linenos|, and most other options not related to syntax highlighting will still function in draft mode. % % Documents can usually be compiled without shell escape in draft mode. The \pkg{ifplatform} package may issue a warning about limited functionality due to shell escape being disabled, but this may be ignored in almost all cases. (Shell escape is only really required if you have an unusual system configuration such that the |\ifwindows| macro must fall back to using shell escape to determine the system. See the \pkg{ifplatform} documentation for more details: \url{http://www.ctan.org/pkg/ifplatform}.) % % If the |cache| option is set, then all existing cache files will be kept while draft mode is on. This allows caching to be used intermitently with draft mode without requiring that the cache be completely recreated each time. Automatic cleanup of cached files will resume as soon as draft mode is turned off. (This assumes that the auxiliary file has not been deleted in the meantime; it contains the cache history and allows automatic cleanup of unused files.) % % % \DescribeMacro{final=\meta{boolean} (default: true)} % This is the opposite of |draft|; it is equivalent to |draft=false|. Again, note that |draft| and |final| settings are typically inherited from the document class. % % % \DescribeMacro{kpsewhich=\meta{boolean} (default: false)} % This option uses |kpsewhich| to locate files that are to be highlighted. Some build tools such as |texi2pdf| function by modifying |TEXINPUTS|; in some cases, users may customize |TEXINPUTS| as well. The |kpsewhich| option allows \pkg{minted} to work with such configurations. % % This option may add a noticeable amount of overhead on some systems, or with some system configurations. % % This option does \emph{not} make \pkg{minted} work with the |-output-directory| and |-aux-directory| command-line options for \LaTeX. For those, see the |outputdir| package option. % % Under Windows, this option currently requires that PowerShell be installed. It may need to be installed in versions of Windows prior to Windows 7. % % % \DescribeMacro{langlinenos=\meta{boolean} (default: false)} % \pkg{minted} uses the \pkg{fancyvrb} package behind the scenes for the code typesetting. \pkg{fancyvrb} provides an option |firstnumber| that allows the starting line number of an environment to be specified. For convenience, there is an option |firstnumber=last| that allows line numbering to pick up where it left off. The |langlinenos| option makes |firstnumber| work for each language individually with all |minted| and |\mint| usages. For example, consider the code and output below. % % \begin{VerbatimOut}[gobble=1]{minted.doc.out} % \begin{minted}[linenos]{python} % def f(x): % return x**2 % \end{minted} % % \begin{minted}[linenos]{ruby} % def func % puts "message" % end % \end{minted} % % \begin{minted}[linenos, firstnumber=last]{python} % def g(x): % return 2*x % \end{minted} % \end{VerbatimOut} % \inputminted{latex}{minted.doc.out} % \hfill % \colorbox{minted@samplebg}{\begin{minipage}{0.6\textwidth} % \input{minted.doc.out} % \end{minipage}} % \hfill\hfill % % Without the |langlinenos| option, the line numbering in the second Python environment would not pick up where the first Python environment left off. Rather, it would pick up with the Ruby line numbering. % % % \DescribeMacro{newfloat=\meta{boolean} (default: false)} % By default, the |listing| environment is created using the \pkg{float} package. The |newfloat| option creates the environment using \pkg{newfloat} instead. This provides better integration with the \pkg{caption} package. % % % \DescribeMacro{outputdir=\meta{directory} (default: \meta{none})} % The |-output-directory| and |-aux-directory| (MiKTeX) command-line options for \LaTeX\ causes problems for \pkg{minted}, because the \pkg{minted} temporary files are saved in ||, but \pkg{minted} still looks for them in the document root directory. There is no way to access the value of the command-line option so that \pkg{minted} can automatically look in the right place. But it is possible to allow the output directory to be specified manually as a package option. % % The output directory should be specified using an absolute path or a path relative to the document root directory. Paths should use forward spaces, even under Windows. Paths that include spaces are not allowed. % % % \DescribeMacro{section} % To control how \LaTeX{} counts the |listing| floats, you can pass either the % |section| or |chapter| option when loading the \pkg{minted} package. % % % \subsection{Macro option usage} % % All \pkg{minted} highlighting commands accept the same set of options. % Options are specified as a comma-separated list of |key=value| pairs. % For example, we can specify that the lines should be numbered: % % \begin{example} % \begin{minted}[linenos=true]{c++} % #include % int main() { % std::cout << "Hello " % << "world" % << std::endl; % } % \end{minted} % \end{example} % % An option value of |true| may also be omitted entirely (including the ``|=|''). % To customize the display of the line numbers further, override the |\theFancyVerbLine| command. % Consult the \pkg{fancyvrb} documentation for details. % % |\mint| accepts the same options: % % \begin{example} % \mint[linenos]{perl}|$x=~/foo/| % \end{example} % % Here's another example: we want to use the \LaTeX{} math mode inside comments: % % \begin{example} % \begin{minted}[mathescape]{python} % # Returns $\sum_{i=1}^{n}i$ % def sum_from_one_to(n): % r = range(1, n + 1) % return sum(r) % \end{minted} % \end{example} % % To make your \LaTeX{} code more readable you might want to indent the code inside a |minted| % environment. % The option |gobble| removes these unnecessary whitespace characters from the output. There is also an |autogobble| option that detects the length of this whitespace automatically. % % \begin{example} % \begin{minted}[gobble=2, % showspaces]{python} % def boring(args = None): % pass % \end{minted} % % versus % % \begin{minted}[showspaces]{python} % def boring(args = None): % pass % \end{minted} % \end{example} % % \DescribeMacro{\setminted} % You may wish to set options for the document as a whole, or for an entire language. This is possible via \cmd\setminted\oarg{language}\marg{key=value,...}. Language-specific options override document-wide options. Individual command and environment options override language-specific options. % % \DescribeMacro{\setmintedinline} % You may wish to set separate options for \cmd\mintinline, either for the document as a whole or for a specific language. This is possible via \cmd\setmintedinline. The syntax is % \cmd\setmintedinline\oarg{language}\marg{key=value,...}. Language-specific options override document-wide options. Individual command options override language-specific options. All settings specified with \cmd\setmintedinline\ override those set with \cmd\setminted. That is, inline settings always have a higher precedence than general settings. % % \subsection{Available options} % % \newcommand\appliesto[1]{\textsf{[For #1 only]}} % % Following is a full list of available options. % For more detailed option descriptions please refer to the \pkg{fancyvrb} and \app{Pygments} documentation. % \begin{optionlist} % \item[autogobble (boolean) (+false+)] % Remove (gobble) all common leading whitespace from code. Essentially a version of +gobble+ that automatically determines what should be removed. Good for code that originally is not indented, but is manually indented after being pasted into a \LaTeX\ document. % % \begin{example} % ...text. % \begin{minted}[autogobble]{python} % def f(x): % return x**2 % \end{minted} % \end{example} % % \item[baselinestretch (+auto+\textbar dimension) (+auto+)] % Value to use as for baselinestretch inside the listing. % % \item[breakafter (string) (+\meta{none}+)] % Break lines after specified characters, not just at spaces, when \texttt{breaklines=true}. For example, \texttt{breakafter=-/} would allow breaks after any hyphens or slashes. Special characters given to \texttt{breakafter} should be backslash-escaped (usually \texttt{\hashchar}, \texttt{\{}, \texttt{\}}, \texttt{\%}, \texttt{[}, \texttt{]}; the backslash \texttt{\textbackslash} may be obtained via \texttt{\textbackslash\textbackslash}). % % \begin{longexample} % \begin{minted}[breaklines, breakafter=d]{python} % some_string = 'SomeTextThatGoesOnAndOnForSoLongThatItCouldNeverFitOnOneLine' % \end{minted} % \end{longexample} % % \item[breakaftergroup] (boolean) (+true+) % When \texttt{breakafter} is used, group all adjacent identical characters together, and only allow a break after the last character. % % \item[breakaftersymbolpre (string) (+\string\,\string\footnotesize\string\ensuremath\{\_\string\rfloor\}, \,\footnotesize\ensuremath{_\rfloor}+)] % The symbol inserted pre-break for breaks inserted by \texttt{breakafter}. % % \item[breakaftersymbolpost (string) (+\meta{none}+)] % The symbol inserted post-break for breaks inserted by \texttt{breakafter}. % % \item[breakanywhere (boolean) (+false+)] % Break lines anywhere, not just at spaces, when \texttt{breaklines=true}. % % \begin{longexample} % \begin{minted}[breaklines, breakanywhere]{python} % some_string = 'SomeTextThatGoesOnAndOnForSoLongThatItCouldNeverFitOnOneLine' % \end{minted} % \end{longexample} % % \item[breakanywheresymbolpre (string) (+\string\,\string\footnotesize\string\ensuremath\{\_\string\rfloor\}, \,\footnotesize\ensuremath{_\rfloor}+)] % The symbol inserted pre-break for breaks inserted by \texttt{breakanywhere}. % % \item[breakanywheresymbolpost (string) (+\meta{none}+)] % The symbol inserted post-break for breaks inserted by \texttt{breakanywhere}. % % \item[breakautoindent (boolean) (+true+)] % When a line is broken, automatically indent the continuation lines to the indentation level of the first line. When \texttt{breakautoindent} and \texttt{breakindent} are used together, the indentations add. This indentation is combined with \texttt{breaksymbolindentleft} to give the total actual left indentation. Does not apply to \texttt{\string\mintinline}. % \item[breakbytoken (boolean) (+false+)] % Only break lines at locations that are not within tokens; prevent tokens from being split by line breaks. By default, \texttt{breaklines} causes line breaking at the space nearest the margin. While this minimizes the number of line breaks that are necessary, it can be inconvenient if a break occurs in the middle of a string or similar token. % % This is not compatible with \texttt{draft} mode. A complete list of Pygments tokens is available at \url{http://pygments.org/docs/tokens/}. If the breaks provided by \texttt{breakbytoken} occur in unexpected locations, it may indicate a bug or shortcoming in the Pygments lexer for the language. % % \item[breakbytokenanywhere (boolean) (+false+)] % Like \texttt{breakbytoken}, but also allows line breaks between immediately adjacent tokens, not just between tokens that are separated by spaces. Using \texttt{breakbytokenanywhere} with \texttt{breakanywhere} is redundant. % \item[breakindent (dimension) (+0pt+)] % When a line is broken, indent the continuation lines by this amount. When \texttt{breakautoindent} and \texttt{breakindent} are used together, the indentations add. This indentation is combined with \texttt{breaksymbolindentleft} to give the total actual left indentation. Does not apply to \texttt{\string\mintinline}. % \item[breaklines (boolean) (+false+)] % Automatically break long lines in \texttt{minted} environments and \texttt{\string\mint} commands, and wrap longer lines in \texttt{\string\mintinline}. By default, automatic breaks occur at space characters. Use \texttt{breakanywhere} to enable breaking anywhere; use \texttt{breakbytoken}, \texttt{breakbytokenanywhere}, and \texttt{breakafter} for more fine-tuned breaking. Using \texttt{escapeinside} to escape to \LaTeX\ and then insert a manual break is also an option. For example, use \texttt{escapeinside=||}, and then insert \texttt{|\textbackslash\textbackslash|} at the appropriate point. (Note that \texttt{escapeinside} does not work within strings.) % % \begin{example} % ...text. % \begin{minted}[breaklines]{python} % def f(x): % return 'Some text ' + str(x) % \end{minted} % \end{example} % % Breaking in \texttt{minted} and \texttt{\string\mint} may be customized in several ways. To customize the indentation of broken lines, see \texttt{breakindent} and \texttt{breakautoindent}. To customize the line continuation symbols, use \texttt{breaksymbolleft} and \texttt{breaksymbolright}. To customize the separation between the continuation symbols and the code, use \texttt{breaksymbolsepleft} and \texttt{breaksymbolsepright}. To customize the extra indentation that is supplied to make room for the break symbols, use \texttt{breaksymbolindentleft} and \texttt{breaksymbolindentright}. Since only the left-hand symbol is used by default, it may also be modified using the alias options \texttt{breaksymbol}, \texttt{breaksymbolsep}, and \texttt{breaksymbolindent}. Note than none of these options applies to \texttt{\string\mintinline}, since they are not relevant in the inline context. % % An example using these options to customize the \texttt{minted} environment is shown below. This uses the \texttt{\string\carriagereturn} symbol from the \pkg{dingbat} package. % % \begingroup % \fvset{breaklines, xleftmargin=2em, xrightmargin=2em} % \begin{longexample} % \begin{minted}[breaklines, % breakautoindent=false, % breaksymbolleft=\raisebox{0.8ex}{ % \small\reflectbox{\carriagereturn}}, % breaksymbolindentleft=0pt, % breaksymbolsepleft=0pt, % breaksymbolright=\small\carriagereturn, % breaksymbolindentright=0pt, % breaksymbolsepright=0pt]{python} % def f(x): % return 'Some text ' + str(x) + ' some more text ' + str(x) + ' even more text that goes on for a while' % \end{minted} % \end{longexample} % \endgroup % % Automatic line breaks are limited with \app{Pygments} styles that use a colored background behind large chunks of text. This coloring is accomplished with \texttt{\string\colorbox}, which cannot break across lines. It may be possible to create an alternative to \texttt{\string\colorbox} that supports line breaks, perhaps with \pkg{TikZ}, but the author is unaware of a satisfactory solution. The only current alternative is to redefine \texttt{\string\colorbox} so that it does nothing. For example, %\begin{Verbatim} %\AtBeginEnvironment{minted}{\renewcommand{\colorbox}[3][]{#3}} %\end{Verbatim} % uses the \pkg{etoolbox} package to redefine \texttt{\string\colorbox} within all \texttt{minted} environments. % % Automatic line breaks will not work with \texttt{showspaces=true} unless you use \texttt{breakanywhere}. You may be able to change the definition of \texttt{\string\FV@Space} if you need this; see the \pkg{fancyvrb} implementation for details. % % \item[breaksymbol (string) (+breaksymbolleft+)] % Alias for \texttt{breaksymbolleft}. % % \item[breaksymbolleft (string) (+\string\tiny\string\ensuremath\{\string\hookrightarrow\}+, {\tiny\ensuremath{\hookrightarrow}})] % The symbol used at the beginning (left) of continuation lines when \texttt{breaklines=true}. To have no symbol, simply set \texttt{breaksymbolleft} to an empty string (``\texttt{=,}'' or ``\texttt{=\{\}}''). The symbol is wrapped within curly braces \texttt{\{\}} when used, so there is no danger of formatting commands such as \texttt{\string\tiny} ``escaping.'' % % The \texttt{\string\hookrightarrow} and \texttt{\string\hookleftarrow} may be further customized by the use of the \texttt{\string\rotatebox} command provided by \pkg{graphicx}. Additional arrow-type symbols that may be useful are available in the \pkg{dingbat} (\texttt{\string\carriagereturn}) and \pkg{mnsymbol} (hook and curve arrows) packages, among others. % % Does not apply to \texttt{\string\mintinline}. % % \item[breaksymbolright (string) (\meta{none})] % The symbol used at breaks (right) when \texttt{breaklines=true}. Does not appear at the end of the very last segment of a broken line. % % \item[breaksymbolindent (dimension) (+breaksymbolindentleft+)] % Alias for \texttt{breaksymbolindentleft}. % % \item[breaksymbolindentleft (dimension) (width of 4 characters in teletype font at default point size)] % The extra left indentation that is provided to make room for \texttt{breaksymbolleft}. This indentation is only applied when there is a \texttt{breaksymbolleft}. % % This may be set to the width of a specific number of (fixed-width) characters by using an approach such as %\begin{Verbatim} %\newdimen\temporarydimen %\settowidth{\temporarydimen}{\ttfamily aaaa} %\end{Verbatim} % and then using \texttt{breaksymbolindentleft=\string\temporarydimen}. % % Does not apply to \texttt{\string\mintinline}. % % \item[breaksymbolindentright (dimension) (width of 4 characters in teletype font at default point size)] % The extra right indentation that is provided to make room for \texttt{breaksymbolright}. This indentation is only applied when there is a \texttt{breaksymbolright}. % % \item[breaksymbolsep (dimension) (+breaksymbolsepleft+)] % Alias for \texttt{breaksymbolsepleft} % % \item[breaksymbolsepleft (dimension) (+1em+)] % The separation between the \texttt{breaksymbolleft} and the adjacent code. Does not apply to \texttt{\string\mintinline}. % % \item[breaksymbolsepright (dimension) (+1em+)] % The separation between the \texttt{breaksymbolright} and the adjacent code. % % \item[bgcolor (string) (\meta{none})] % Background color of the listing. % Notice that the value of this option must \emph{not} be a color command. Instead, it must be a color % \emph{name}, given as a string, of a previously-defined color: % % \begin{example} % \definecolor{bg}{rgb}{0.95,0.95,0.95} % \begin{minted}[bgcolor=bg]{php} % % \end{minted} % \end{example} % % This option puts \texttt{minted} environments and \texttt{\string\mint} commands in a minipage with a colored background. It puts \texttt{\string\mintinline} inside a \texttt{\string\colorbox}. If you want to use \texttt{\string\setminted} to set background colors, and only want background colors on \texttt{minted} and \texttt{\string\mint}, you may use \texttt{\string\setmintedinline\{bgcolor=\{\}\}} to turn off the coloring for inline commands. % % \textbf{This option will prevent \texttt{breaklines} from working with \texttt{\string\mintinline}.} A \texttt{\string\colorbox} cannot break across lines. % % \textbf{This option will prevent environments from breaking across pages.} If you want support for page breaks and advanced options, you should consider a framing package such as \pkg{framed}, \pkg{mdframed}, or \pkg{tcolorbox}. It is easy to add framing to \pkg{minted} commands and environments using the \pkg{etoolbox} package. For example, using \pkg{mdframed}: %\begin{Verbatim} %\BeforeBeginEnvironment{minted}{\begin{mdframed}} %\AfterEndEnvironment{minted}{\end{mdframed}} %\end{Verbatim} % Some framing packages also provide built-in commands for such purposes. For example, \pkg{mdframed} provides a \texttt{\string\surroundwithmdframed} command, which could be used to add a frame to all \texttt{minted} environments: %\begin{Verbatim} %\surroundwithmdframed{minted} %\end{Verbatim} % \pkg{tcolorbox} even provides a built-in framing environment with \pkg{minted} support. % % \item[codetagify (list of strings) (highlight +XXX+, +TODO+, +BUG+, and +NOTE+)] % Highlight special code tags in comments and docstrings. % \item[encoding (string) (system-specific)] % Sets the file encoding that \app{Pygments} expects. See also +outencoding+. % \item[escapeinside (string) (\meta{none})] % Escape to \LaTeX\ between the two characters specified in \texttt{\string(string\string)}. All code between the two characters will be interpreted as \LaTeX\ and typeset accordingly. This allows for additional formatting. The escape characters need not be identical. Special \LaTeX\ characters must be escaped when they are used as the escape characters (for example, \texttt{escapeinside=\textbackslash\#\textbackslash\%}). Requires \app{Pygments} 2.0\string+. % % \textbf{Escaping does not work inside strings and comments (for comments, there is \texttt{texcomments}). As of Pygments 2.0.2, this means that escaping is ``fragile'' with some lexers.} Due to the way that Pygments implements \texttt{escapeinside}, any ``escaped'' \LaTeX\ code that resembles a string or comment for the current lexer may break \texttt{escapeinside}. There is a \href{https://bitbucket.org/birkenfeld/pygments-main/issue/1118}{Pygments issue} for this case. Additional details and a limited workaround for some scenarios are available on the \href{https://github.com/gpoore/minted/issues/70#issuecomment-111729930}{\pkg{minted} GitHub site}. % % \begin{example} % \begin{minted}[escapeinside=||]{py} % def f(x): % y = x|\colorbox{green}{**}|2 % return y % \end{minted} % \end{example} % % \textbf{Note that when math is used inside escapes, in a few cases ligature handling may need to be modified.} The single-quote character (\texttt{\textquotesingle}) is normally a shortcut for \texttt{\string^\string\prime} in math mode, but this is disabled in verbatim content as a byproduct of ligatures being disabled. For the same reason, any package that relies on active characters in math mode (for example, \pkg{icomma}) will produce errors along the lines of \texttt{TeX capacity exceeded} and \texttt{\string\leavevmode \string\kern \string\z@}. This may be fixed by modifying \texttt{\string\@noligs}, as described at \url{http://tex.stackexchange.com/questions/223876}. \pkg{minted} currently does not attempt to patch \texttt{\string\@noligs} due to the potential for package conflicts. % \item[firstline (integer) (+1+)] % The first line to be shown. % All lines before that line are ignored and do not appear in the output. % \item[firstnumber (+auto+\textbar integer) (+auto+ = 1)] % Line number of the first line. % \item[fontfamily (family name) (+tt+)] % The font family to use. % +tt+, +courier+ and +helvetica+ are pre-defined. % \item[fontseries (series name) (+auto+ -- the same as the current font)] % The font series to use. % \item[fontsize (font size) (+auto+ -- the same as the current font)] % The size of the font to use, as a size command, e.g. +\footnotesize+. % \item[fontshape (font shape) (+auto+ -- the same as the current font)] % The font shape to use. % \item[formatcom (command) (\meta{none})] % A format to execute before printing verbatim text. % \item[frame (+none+\textbar +leftline+\textbar +topline+\textbar +bottomline+\textbar +lines+\textbar +single+) (+none+)] % The type of frame to put around the source code listing. % \item[framerule (dimension) (+0.4pt+)] % Width of the frame. % \item[framesep (dimension) (\cmd\fboxsep)] % Distance between frame and content. % \item[funcnamehighlighting (boolean) (+true+)] \appliesto{PHP} % If +true+, highlights built-in function names. % \item[gobble (integer) (+0+)] % Remove the first $n$ characters from each input line. % \item[keywordcase] (string) (+'lower'+) % Changes capitalization of keywords. Takes +'lower'+, +'upper'+, or +'capitalize'+. % \item[label (string) (\emph{empty})] % Add a label to the top, the bottom or both of the frames around the code. % See the \pkg{fancyvrb} documentation for more information and examples. % \emph{Note:} This does \emph{not} add a +\label+ to the current listing. % To achieve that, use a floating environment (section \ref{sec:float}) instead. % \item[labelposition (+none+\textbar +topline+\textbar +bottomline+\textbar +all+) (+topline+, +all+ or \emph{none})] % Position where to print the label (see above; default: +topline+ if one label is defined, +all+ if two are defined, \emph{none} else). % See the \pkg{fancyvrb} documentation for more information. % \item[lastline (integer) (\emph{last line of input})] % The last line to be shown. % \item[linenos (boolean) (+false+)] % Enables line numbers. % In order to customize the display style of line numbers, you need to redefine the +\theFancyVerbLine+ % macro: % % \begin{example} % \renewcommand{\theFancyVerbLine}{\sffamily % \textcolor[rgb]{0.5,0.5,1.0}{\scriptsize % \oldstylenums{\arabic{FancyVerbLine}}}} % % \begin{minted}[linenos, % firstnumber=11]{python} % def all(iterable): % for i in iterable: % if not i: % return False % return True % \end{minted} % \end{example} % % \item[numbers (+left+\textbar+right+) (\emph{none})] % Essentially the same as +linenos+, except the side on which the numbers appear may be specified. % \item[mathescape (boolean) (+false+)] % Enable \LaTeX{} math mode inside comments. % Usage as in package \pkg{listings}. % See the note under \texttt{escapeinside} regarding math and ligatures. % \item[numberblanklines (boolean) (+true+)] % Enables or disables numbering of blank lines. % \item[numbersep (dimension) (+12pt+)] % Gap between numbers and start of line. % \item[obeytabs (boolean) (+false+)] % Treat tabs as tabs instead of converting them to spaces. % \item[outencoding (string) (system-specific)] % Sets the file encoding that \app{Pygments} uses for highlighted output. Overrides any encoding previously set via +encoding+. % \item[python3 (boolean) (+false+)] \appliesto{PythonConsoleLexer} % Specifies whether Python 3 highlighting is applied. % \item[resetmargins (boolean) (+false+)] % Resets the left margin inside other environments. % \item[rulecolor (color command) (\emph{black})] % The color of the frame. % \item[samepage (boolean) (+false+)] % Forces the whole listing to appear on the same page, even if it doesn't fit. % \item[showspaces (boolean) (+false+)] % Enables visible spaces: \verb*/visible spaces/. % \item[showtabs (boolean) (+false+)] % Enables visible tabs---only works in combination with +obeytabs+. % \item[startinline (boolean) (+false+)] \appliesto{PHP} % Specifies that the code starts in PHP mode, i.e., leading + % T id(T value) { % return value; % } % \end{cppcode} % \end{example} % % If you want to provide extra options on the fly, or override existing default options, you can do that, too: % % \begin{example} % \newminted{cpp}{gobble=2,linenos} % % \begin{cppcode*}{linenos=false, % frame=single} % int const answer = 42; % \end{cppcode*} % \end{example} % % Notice the star ``|*|'' behind the environment name---due to restrictions in \pkg{fancyvrb}'s handling % of options, it is necessary to provide a \emph{separate} environment that accepts options, and the options % are \emph{not} optional on the starred version of the environment. % % The default name of the environment is \meta{language}|code|. % If this name clashes with another environment or if you want to choose an own name for another reason, you may % do so by specifying it as the first argument: \cmd\newminted\oarg{environment name}\marg{language}\marg{options}. % % \DescribeMacro{\newmint} % The above macro only defines shortcuts for the |minted| environment. % The main reason is that the short command form |\mint| often needs different options---at the very least, it % will generally not use the |gobble| option. % A shortcut for |\mint| is defined using \cmd\newmint\oarg{macro name}\marg{language}\marg{options}. % The arguments and usage are identical to |\newminted|. % If no \meta{macro name} is specified, \meta{language} is used. % % \begin{example} % \newmint{perl}{showspaces} % % \perl/my $foo = $bar;/ % \end{example} % % % \DescribeMacro{\newmintinline} % This creates custom versions of \cmd\mintinline. The syntax is the same as that for \cmd\newmint: \cmd\newmintinline\oarg{macro~name}\marg{language}\marg{options}. If a \meta{macro~name} is not specified, then the created macro is called |\|\meta{language}|inline|. % % \begin{example} % \newmintinline{perl}{showspaces} % % X\perlinline/my $foo = $bar;/X % \end{example} % % \DescribeMacro{\newmintedfile} % This creates custom versions of \cmd\inputminted. The syntax is % \begin{center} % \cmd\newmintedfile\oarg{macro~name}\marg{language}\marg{options} % \end{center} % If no \meta{macro name} is given, then the macro is called |\|\meta{language}|file|. % % % % \section{FAQ and Troubleshooting} % % In some cases, \pkg{minted} may not give the desired result due to other document settings that it cannot control. Common issues are described below, with workarounds or solutions. You may also wish to search \href{http://tex.stackexchange.com/}{tex.stackexchange.com} or ask a question there, if you are working with \pkg{minted} in a non-typical context. % % \begin{itemize} % \item \textbf{I receive a ``Too many open files'' error under OS X when using caching.} See the note on OS X under Section~\ref{sec:basic:preliminary}. % \item \textbf{When I use \pkg{minted} with KOMA-Script document classes, I get warnings about \texttt{\string\float@addtolists}.} \pkg{minted} uses the \pkg{float} package to produce floated listings, but this conflicts with the way KOMA-Script does floats. Load the package \pkg{scrhack} to resolve the conflict. Or use \pkg{minted}'s |newfloat| package option. % \item \textbf{Tilde characters \texttt{\string~} are raised, almost like superscripts.} % This is a font issue. You need a different font encoding, possibly with a different font. Try |\usepackage[T1]{fontenc}|, perhaps with |\usepackage{lmodern}|, or something similar. % % \item \textbf{I'm getting errors with math, something like \texttt{TeX capacity exceeded} and \texttt{\string\leavevmode \string\kern \string\z@}.} This is due to ligatures being disabled within verbatim content. See the note under |escapeinside|. % % \item \textbf{Quotation marks and backticks don't look right. Backtick characters \texttt{\string`} are appearing as left quotes. Single quotes are appearing as curly right quotes.} % This is due to how Pygments outputs \LaTeX\ code, combined with how \LaTeX\ deals with verbatim content. Try |\usepackage{upquote}|. % % \item \textbf{I'm getting errors with Beamer.} Due to how Beamer treats verbatim content, you may need to use either the |fragile| or |fragile=singleslide| options for frames that contain \pkg{minted} commands and environments. |fragile=singleslide| works best, but it disables overlays. |fragile| works by saving the contents of each frame to a temp file and then reusing them. This approach allows overlays, but will break if you have the string |\end{frame}| at the beginning of a line (for example, in a |minted| environment). To work around that, you can indent the content of the environment (so that the |\end{frame}| is preceded by one or more spaces) and then use the |gobble| or |autogobble| options to remove the indentation. % % \item \textbf{Tabs are eaten by Beamer.} This is due to \href{https://bitbucket.org/rivanvx/beamer/issue/310/tab-characters-in-listings-lost-when-using}{a bug in Beamer's treatment of verbatim content}. Upgrade Beamer or use the linked patch. Otherwise, try |fragile=singleslide| if you don't need overlays, or consider using \cmd\inputminted\ or converting the tabs into spaces. % % \item \textbf{I'm trying to create several new \pkg{minted} commands/environments, and want them all to have the same settings. I'm saving the settings in a macro and then using the macro when defining the commands/environments. But it's failing.} % This is due to the way that \pkg{keyval} works (\pkg{minted} uses it to manage options). Arguments are not expanded. See \href{http://tex.stackexchange.com/questions/13563/building-keyval-arguments-using-a-macro/13564#13564}{this} and \href{http://tex.stackexchange.com/questions/145363/why-does-includegraphics-varone-vartwo-not-compile/145366#145366}{this} for more information. It is still possible to do what you want; you just need to expand the options macro before passing it to the commands that create the new commands/environments. An example is shown below. The |\expandafter| is the vital part. %\begin{verbatim} %\def\args{linenos,frame=single,fontsize=\footnotesize,style=bw} % %\newcommand{\makenewmintedfiles}[1]{% % \newmintedfile[inputlatex]{latex}{#1}% % \newmintedfile[inputc]{c}{#1}% %} % %\expandafter\makenewmintedfiles\expandafter{\args} %\end{verbatim} % % \item \textbf{I want to use \texttt{\string\mintinline} in a context that normally doesn't allow verbatim content.} % The |\mintinline| command will already work in many places that do not allow normal verbatim commands like |\verb|, so make sure to try it first. If it doesn't work, one of the simplest alternatives is to save your code in a box, and then use it later. For example, %\begin{verbatim} %\newsavebox\mybox %\begin{lrbox}{\mybox} %\mintinline{cpp}{std::cout} %\end{lrbox} % %\commandthatdoesnotlikeverbatim{Text \usebox{\mybox}} %\end{verbatim} % % \item \textbf{Extended characters do not work inside \pkg{minted} commands and environments, even when the \pkg{inputenc} package is used.} % Version 2.0 adds support for extended characters under the pdfTeX engine. But if you need characters that are not supported by \pkg{inputenc}, you should use the XeTeX or LuaTeX engines instead. % % \item \textbf{The \pkg{polyglossia} package is doing undesirable things to code. (For example, adding extra space around colons in French.)} You may need to put your code within |\begin{english}...\end{english}|. This may done for all |minted| environments using \pkg{etoolbox} in the preamble: % \begin{verbatim} %\usepackage{etoolbox} %\BeforeBeginEnvironment{minted}{\begin{english}} %\AfterEndEnvironment{minted}{\end{english}} % \end{verbatim} % % \item \begin{sloppypar} \textbf{Tabs are being turned into the character sequence \texttt{\string^\string^I}}. % This happens when you use XeLaTeX. You need to use the |-8bit| command-line option so that tabs may be written correctly to temporary files. See \url{http://tex.stackexchange.com/questions/58732/how-to-output-a-tabulation-into-a-file} for more on XeLaTeX's handling of tab characters. \end{sloppypar} % % \item \textbf{The \pkg{caption} package produces an error when \texttt{\string\captionof} and other commands are used in combination with \pkg{minted}.} % Load the \pkg{caption} package with the option |compatibility=false|. Or better yet, use \pkg{minted}'s |newfloat| package option, which provides better \pkg{caption} compatibility. % % \item \textbf{I need a listing environment that supports page breaks.} The built-in listing environment is a standard float; it doesn't support page breaks. You will probably want to define a new environment for long floats. For example, %\begin{verbatim} %\usepackage{caption} %\newenvironment{longlisting}{\captionsetup{type=listing}}{} %\end{verbatim} % With the \pkg{caption} package, it is best to use \pkg{minted}'s |newfloat| package option. See \url{http://tex.stackexchange.com/a/53540/10742} for more on |listing| environments with page breaks. % % \item \textbf{I want to use a custom script/executable to access Pygments, rather than |pygmentize|.} Redefine |\MintedPygmentize|: %\begin{verbatim} %\renewcommand{\MintedPygmentize}{...} %\end{verbatim} % % \item \textbf{I want to use the command-line option \texttt{-output-directory}, or MiKTeX's \texttt{-aux-directory}, but am getting errors.} Use the package option |outputdir| to specify the location of the output directory. Unfortunately, there is no way for \pkg{minted} to detect the output directory automatically. % % \item \textbf{I want extended characters in frame labels, but am getting errors.} This can happen with \pkg{minted} <2.0 and Python 2.7, due to a \href{https://bitbucket.org/birkenfeld/pygments-main/issue/801/python-2-fails-to-detect-terminal-encoding}{terminal encoding issue with Pygments}. It should work with any version of Python with \pkg{minted} 2.0+, which processes labels internally and does not send them to Python. % \item \textbf{\texttt{minted} environments have extra vertical space inside \texttt{tabular}.} It is possible to \href{https://github.com/gpoore/minted/issues/82}{create a custom environment} that eliminates the extra space. However, a general solution that behaves as expected in the presence of adjacent text remains to be found. % \end{itemize} % % % % % \section*{Acknowledgements} % % Konrad Rudolph: Special thanks to Philipp Stephani and the rest of the guys from \texttt{comp.text.tex} and \texttt{tex.stackexchange.com}. % % Geoffrey Poore: Thanks to Marco Daniel for the code on \url{tex.stackexchange.com} that inspired automatic line breaking. % % \PrintChangelog % % \StopEventually{} %\StopEventually{\PrintIndex} % % % \section{Implementation} % % \iffalse %<*package> % \fi % % % \subsection{Required packages} % Load required packages. For compatibility reasons, most old functionality should be supported with the original set of packages. More recently added packages, such as |etoolbox| and |xstring|, should only be used for new features when possible. % \begin{macrocode} \RequirePackage{keyval} \RequirePackage{kvoptions} \RequirePackage{fancyvrb} \RequirePackage{float} \RequirePackage{ifthen} \RequirePackage{calc} \RequirePackage{ifplatform} \RequirePackage{pdftexcmds} \RequirePackage{etoolbox} \RequirePackage{xstring} \RequirePackage{lineno} % \end{macrocode} % % Make sure that either |color| or |xcolor| is loaded by the beginning of the document. % \begin{macrocode} \AtBeginDocument{% \@ifpackageloaded{color}{}{% \@ifpackageloaded{xcolor}{}{\RequirePackage{xcolor}}}% } % \end{macrocode} % % % % \subsection{Package options} % % \begin{macro}{\minted@float@within} % % Define an option that controls the section numbering of the |listing| float. % % \begin{macrocode} \DeclareVoidOption{chapter}{\def\minted@float@within{chapter}} \DeclareVoidOption{section}{\def\minted@float@within{section}} % \end{macrocode} % \end{macro} % % % \begin{macro}{newfloat} % % Define an option to use \pkg{newfloat} rather than \pkg{float} to create a floated |listing| environment. % % \begin{macrocode} \DeclareBoolOption{newfloat} % \end{macrocode} % \end{macro} % % % \begin{macro}{cache} % Define an option that determines whether highlighted content is cached. We use a boolean to keep track of its state. % \begin{macrocode} \DeclareBoolOption[true]{cache} % \end{macrocode} % \end{macro} % % % \begin{macro}{\minted@jobname} % At various points, temporary files and directories will need to be named after the main |.tex| file. The typical way to do this is to use |\jobname|. However, if the file name contains spaces, then |\jobname| will contain the name wrapped in quotes (older versions of MiKTeX replace spaces with asterisks instead, and \texttt{XeTeX} apparently \href{http://tex.stackexchange.com/a/93829/10742}{allows double quotes within file names}, in which case names are wrapped in single quotes}). While that is perfectly fine for working with \LaTeX\ internally, it causes problems with |\write18|, since quotes will end up in unwanted locations in shell commands. It would be possible to strip the wrapping quotation marks when they are present, and maintain any spaces in the file name. But it is simplest to create a ``sanitized'' version of |\jobname| in which spaces and asterisks are replaced by underscores, and double quotes are stripped. % \begin{macrocode} \StrSubstitute{\jobname}{ }{_}[\minted@jobname] \StrSubstitute{\minted@jobname}{*}{_}[\minted@jobname] \StrSubstitute{\minted@jobname}{"}{}[\minted@jobname] % \end{macrocode} % \end{macro} % % % \begin{macro}{\minted@cachedir} % Set the directory in which cached content is saved. The default uses a |minted-| prefix followed by the sanitized |\minted@jobname|. % \begin{macrocode} \newcommand{\minted@cachedir}{\detokenize{_}minted-\minted@jobname} \let\minted@cachedir@windows\minted@cachedir \define@key{minted}{cachedir}{% \@namedef{minted@cachedir}{#1}% \StrSubstitute{\minted@cachedir}{/}{\@backslashchar}[\minted@cachedir@windows]} % \end{macrocode} % \end{macro} % % % \begin{macro}{\minted@outputdir} % The |-output-directory| command-line option for \LaTeX\ causes problems for \pkg{minted}, because the \pkg{minted} temporary files are saved in the output directory, but \pkg{minted} still looks for them in the document root directory. There is no way to access the value of the command-line option. But it is possible to allow the output directory to be specified manually as a package option. A trailing slash is automatically appended to the |outputdir|, so that it may be directly joined to |cachedir|. This may be redundant if the user-supplied value already ends with a slash, but doubled slashes are ignored under *nix and Windows, so it isn't a problem. % \begin{macrocode} \let\minted@outputdir\@empty \let\minted@outputdir@windows\@empty \define@key{minted}{outputdir}{% \@namedef{minted@outputdir}{#1/}% \StrSubstitute{\minted@outputdir}{/}% {\@backslashchar}[\minted@outputdir@windows]} % \end{macrocode} % \end{macro} % % % \begin{macro}{kpsewhich} % Define an option that invokes |kpsewhich| to locate the files that are to be |pygmentize|d. This isn't done by default to avoid the extra overhead, but can be useful with some build tools such as |texi2pdf| that rely on modifying |TEXINPUTS|. % \begin{macrocode} \DeclareBoolOption{kpsewhich} % \end{macrocode} % \end{macro} % % % \begin{macro}{langlinenos} % Define an option that makes all |minted| environments and |\mint| commands for a given language share cumulative line numbering (if |firstnumber=last|). % \begin{macrocode} \DeclareBoolOption{langlinenos} % \end{macrocode} % \end{macro} % % % \begin{macro}{draft} % Define an option that allows \pkg{fancyvrb} to do all typesetting directly, without using \app{Pygments}. This trades syntax highlighting for speed. Note that in many cases, the difference in performance between caching and draft mode will be minimal. Also note that draft settings may be inherited from the document class. % \begin{macrocode} \DeclareBoolOption{draft} % \end{macrocode} % \end{macro} % % \begin{macro}{final} % Define a |final| option that is the opposite of |draft|, since many packages do this. % \begin{macrocode} \DeclareComplementaryOption{final}{draft} % \end{macrocode} % \end{macro} % % % Process package options. Proceed with everything that immediately relies upon them. If PGF/Ti\textit{k}Z externalization is in use, switch on |draft| mode and turn off |cache|. Externalization involves compiling the \emph{entire} document; all parts not related to the current image are ``silently thrown away.'' \pkg{minted} needs to cooperate with that by not writing any temp files or creating any directories. % % \begin{macrocode} \ProcessKeyvalOptions* \ifthenelse{\boolean{minted@newfloat}}{\RequirePackage{newfloat}}{} \ifcsname tikzexternalrealjob\endcsname \minted@drafttrue \minted@cachefalse \else \fi \ifthenelse{\boolean{minted@cache}}{% \AtEndOfPackage{\ProvideDirectory{\minted@outputdir\minted@cachedir}}}{} % \end{macrocode} % % % % \subsection{Input, caching, and temp files} % % \begin{macro}{\minted@input} % We need a wrapper for |\input|. In most cases, |\input| failure will be due to attempts to use |\inputminted| with files that don't exist, but we also want to give informative error messages when |outputdir| is needed or incompatible build tools are used. % \begin{macrocode} \newcommand{\minted@input}[1]{% \IfFileExists{#1}% {\input{#1}}% {\PackageError{minted}{Missing Pygments output; \string\inputminted\space was^^Jprobably given a file that does not exist--otherwise, you may need ^^Jthe outputdir package option, or may be using an incompatible build tool\ifwindows,^^Jor may be using the kpsewhich option without having PowerShell installed\fi}% {This could be caused by using -output-directory or -aux-directory ^^Jwithout setting minted's outputdir, or by using a build tool that ^^Jchanges paths in ways minted cannot detect\ifwindows, or by using the ^^Jkpsewhich option without PowerShell\fi.}}% } % \end{macrocode} % \end{macro} % % % \begin{macro}{\minted@infile} % Define a default name for files of highlighted content that are brought it. Caching will redefine this. We start out with the default, non-caching value. % \begin{macrocode} \newcommand{\minted@infile}{\minted@jobname.out.pyg} % \end{macrocode} % \end{macro} % % % We need a way to track the cache files that are created, and delete those that are not in use. This is accomplished by creating a comma-delimited list of cache files and saving this list to the |.aux| file so that it may be accessed on subsequent runs. During subsequent runs, this list is compared against the cache files that are actually used, and unused files are deleted. Cache file names are created with MD5 hashes of highlighting settings and file contents, with a |.pygtex| extension, so they never contain commas. Thus comma-delimiting the list of file names doesn't introduce a potential for errors. % % \begin{macro}{\minted@cachelist} % This is a list of the current cache files. % \begin{macrocode} \newcommand{\minted@cachelist}{} % \end{macrocode} % \end{macro} % % \begin{macro}{\minted@addcachefile} % This adds a file to the list of cache files. It also creates a macro involving the hash, so that the current usage of the hash can be easily checked by seeing if the macro exists. The list of cache files must be created with built-in linebreaks, so that when it is written to the |.aux| file, it won't all be on one line and thereby risk buffer errors. % \begin{macrocode} \newcommand{\minted@addcachefile}[1]{% \expandafter\long\expandafter\gdef\expandafter\minted@cachelist\expandafter{% \minted@cachelist,^^J% \space\space#1}% \expandafter\gdef\csname minted@cached@#1\endcsname{}% } % \end{macrocode} % \end{macro} % % % \begin{macro}{\minted@savecachelist} % % We need to be able to save the list of cache files to the |.aux| file, so that we can reload it on the next run. % \begin{macrocode} \newcommand{\minted@savecachelist}{% \ifdefempty{\minted@cachelist}{}{% \immediate\write\@mainaux{% \string\gdef\string\minted@oldcachelist\string{% \minted@cachelist\string}}% }% } % \end{macrocode} % \end{macro} % % % \begin{macro}{\minted@cleancache} % Clean up old cache files that are no longer in use. % \begin{macrocode} \newcommand{\minted@cleancache}{% \ifcsname minted@oldcachelist\endcsname \def\do##1{% \ifthenelse{\equal{##1}{}}{}{% \ifcsname minted@cached@##1\endcsname\else \DeleteFile[\minted@outputdir\minted@cachedir]{##1}% \fi }% }% \expandafter\docsvlist\expandafter{\minted@oldcachelist}% \else \fi } % \end{macrocode} % \end{macro} % % % At the end of the document, save the list of cache files and clean the cache. If in draft mode, don't clean up the cache and save the old cache file list for next time. This allows draft mode to be switched on and off without requiring that all highlighted content be regenerated. The saving and cleaning operations may be called without conditionals, since their definitions already contain all necessary checks for their correct operation. % \begin{macrocode} \ifthenelse{\boolean{minted@draft}}% {\AtEndDocument{% \ifcsname minted@oldcachelist\endcsname \let\minted@cachelist\minted@oldcachelist \minted@savecachelist \fi}}% {\AtEndDocument{% \minted@savecachelist \minted@cleancache}}% % \end{macrocode} % % % % \subsection{OS interaction} % % We need system-dependent macros for communicating with the ``outside world.'' % % \begin{macro}{\DeleteFile} % % Delete a file. Define conditionally in case an equivalent macro has already been defined. % % \begin{macrocode} \ifwindows \providecommand{\DeleteFile}[2][]{% \ifthenelse{\equal{#1}{}}% {\IfFileExists{#2}{\immediate\write18{del "#2"}}{}}% {\IfFileExists{#1/#2}{% \StrSubstitute{#1}{/}{\@backslashchar}[\minted@windir] \immediate\write18{del "\minted@windir\@backslashchar #2"}}{}}} \else \providecommand{\DeleteFile}[2][]{% \ifthenelse{\equal{#1}{}}% {\IfFileExists{#2}{\immediate\write18{rm "#2"}}{}}% {\IfFileExists{#1/#2}{\immediate\write18{rm "#1/#2"}}{}}} \fi % \end{macrocode} % \end{macro} % % % \begin{macro}{\ProvideDirectory} % % We need to be able to create a directory, if it doesn't already exist. This is primarily for storing cached highlighted content. % % \begin{macrocode} \ifwindows \newcommand{\ProvideDirectory}[1]{% \StrSubstitute{#1}{/}{\@backslashchar}[\minted@windir] \immediate\write18{if not exist "\minted@windir" mkdir "\minted@windir"}} \else \newcommand{\ProvideDirectory}[1]{% \immediate\write18{mkdir -p "#1"}} \fi % \end{macrocode} % \end{macro} % % % \begin{macro}{\TestAppExists} % % Determine whether a given application exists. % % Usage is a bit roundabout, but has been retained for backward compatibility. At some point, it may be worth replacing this with something using \verb`\@@input"|"`. That would require MiKTeX users to |--enable-pipes|, however, which would make things a little more complicated. If Windows XP compatibility is ever no longer required, the |where| command could be used instead of the approach for Windows. % % To test whether an application exists, use the following code: % % \begin{Verbatim} %\TestAppExists{appname} %\ifthenelse{\boolean{AppExists}}{app exists}{app doesn't exist} % \end{Verbatim} % % \begin{macrocode} \newboolean{AppExists} \newread\minted@appexistsfile \newcommand{\TestAppExists}[1]{ \ifwindows % \end{macrocode} % % On Windows, we need to use path expansion and write the result to a file. % If the application doesn't exist, the file will be empty (except for a newline); % otherwise, it will contain the full path of the application. % % \begin{macrocode} \DeleteFile{\minted@jobname.aex} \immediate\write18{for \string^\@percentchar i in (#1.exe #1.bat #1.cmd) do set >"\minted@jobname.aex" >"\minted@jobname.aex"} %$ <- balance syntax highlighting \immediate\openin\minted@appexistsfile\minted@jobname.aex \expandafter\def\expandafter\@tmp@cr\expandafter{\the\endlinechar} \endlinechar=-1\relax \readline\minted@appexistsfile to \minted@apppathifexists \endlinechar=\@tmp@cr \ifthenelse{\equal{\minted@apppathifexists}{}} {\AppExistsfalse} {\AppExiststrue} \immediate\closein\minted@appexistsfile \DeleteFile{\minted@jobname.aex} \else % \end{macrocode} % % On Unix-like systems, we do a straightforward |which| test and create a file upon success, whose existence we can then check. % % \begin{macrocode} \immediate\write18{which "#1" && touch "\minted@jobname.aex"} \IfFileExists{\minted@jobname.aex} {\AppExiststrue \DeleteFile{\minted@jobname.aex}} {\AppExistsfalse} \fi } % \end{macrocode} % \end{macro} % % % \subsection{Option processing} % % Option processing is somewhat involved, because we want to be able to define options at various levels of hierarchy: individual command/environment, language, global (document). And once those options are defined, we need to go through the hierarchy in a defined order of precedence to determine which option to apply. As if that wasn't complicated enough, some options need to be sent to Pygments, some need to be sent to \pkg{fancyvrb}, and some need to be processed within \pkg{minted} itself. % % To begin with, we need macros for storing lists of options that will later be passed via the command line to Pygments (|optlistcl|). These are defined at the global (|cl@g|), language (|cl@lang|), and command or environment (|cl@cmd|) levels, so that settings can be specified at various levels of hierarchy. The language macro is actually a placeholder. The current language will be tracked using |\minted@lang|. Each individual language will create a |\minted@optlistcl@lang|\meta{language} macro. |\minted@optlistcl@lang| may be |\let| to this macro as convenient; otherwise, the general language macro merely serves as a placeholder. % % The global- and language-level lists also have an |inline| (|i|) variant. This allows different settings to be applied in inline settings. An inline variant is not needed at the command/environment level, since at that level settings would not be present unless they were supposed to be applied. % % \begin{macro}{\minted@optlistcl@g} % \begin{macrocode} \newcommand{\minted@optlistcl@g}{} % \end{macrocode} % \end{macro} % % \begin{macro}{\minted@optlistcl@g@i} % \begin{macrocode} \newcommand{\minted@optlistcl@g@i}{} % \end{macrocode} % \end{macro} % % \begin{macro}{\minted@lang} % \begin{macrocode} \let\minted@lang\@empty % \end{macrocode} % \end{macro} % % \begin{macro}{\minted@optlistcl@lang} % \begin{macrocode} \newcommand{\minted@optlistcl@lang}{} % \end{macrocode} % \end{macro} % % \begin{macro}{\minted@optlistcl@lang@i} % \begin{macrocode} \newcommand{\minted@optlistcl@lang@i}{} % \end{macrocode} % \end{macro} % % \begin{macro}{\minted@optlistcl@cmd} % \begin{macrocode} \newcommand{\minted@optlistcl@cmd}{} % \end{macrocode} % \end{macro} % % We also need macros for storing lists of options that will later be passed to \pkg{fancyvrb} (|optlistfv|). As before, these exist at the global (|fv@g|), language (|fv@lang|), and command or environment (|fv@cmd|) levels. Pygments accepts \pkg{fancyvrb} options, but in almost all cases, these options may be applied via |\fvset| rather than via running Pygments. This is significantly more efficient when caching is turned on, since it allows formatting changes to be applied without having to re-highlight the code. % % \begin{macro}{\minted@optlistfv@g} % \begin{macrocode} \newcommand{\minted@optlistfv@g}{} % \end{macrocode} % \end{macro} % % \begin{macro}{\minted@optlistfv@g@i} % \begin{macrocode} \newcommand{\minted@optlistfv@g@i}{} % \end{macrocode} % \end{macro} % % \begin{macro}{\minted@optlistfv@lang} % \begin{macrocode} \newcommand{\minted@optlistfv@lang}{} % \end{macrocode} % \end{macro} % % \begin{macro}{\minted@optlistfv@lang@i} % \begin{macrocode} \newcommand{\minted@optlistfv@lang@i}{} % \end{macrocode} % \end{macro} % % \begin{macro}{\minted@optlistfv@cmd} % \begin{macrocode} \newcommand{\minted@optlistfv@cmd}{} % \end{macrocode} % \end{macro} % % \begin{macro}{\minted@configlang} % % We need a way to check whether a language has had all its option list macros created. This generally occurs in a context where |\minted@lang| needs to be set. So we create a macro that does both at once. If the language list macros do not exist, we create them globally to simplify future operations. % \begin{macrocode} \newcommand{\minted@configlang}[1]{% \def\minted@lang{#1}% \ifcsname minted@optlistcl@lang\minted@lang\endcsname\else \expandafter\gdef\csname minted@optlistcl@lang\minted@lang\endcsname{}% \fi \ifcsname minted@optlistcl@lang\minted@lang @i\endcsname\else \expandafter\gdef\csname minted@optlistcl@lang\minted@lang @i\endcsname{}% \fi \ifcsname minted@optlistfv@lang\minted@lang\endcsname\else \expandafter\gdef\csname minted@optlistfv@lang\minted@lang\endcsname{}% \fi \ifcsname minted@optlistfv@lang\minted@lang @i\endcsname\else \expandafter\gdef\csname minted@optlistfv@lang\minted@lang @i\endcsname{}% \fi } % \end{macrocode} % \end{macro} % % We need a way to define options in bulk at the global, language, and command levels. How this is done will depend on the type of option. The keys created are grouped by level: |minted@opt@g|, |minted@opt@lang|, and |minted@opt@cmd|, plus inline variants. The language-level key groupings use |\minted@lang| internally, so we don't need to duplicate the internals for different languages. The key groupings are independent of whether a given option relates to Pygments, \pkg{fancyvrb}, etc. Organization by level is the only thing that is important here, since keys are applied in a hierarchical fashion. Key values are stored in macros of the form |\minted@opt@|\meta{level}|:|\meta{key}, so that they may be retrieved later. In practice, these key macros will generally not be used directly (hence the colon in the name). Rather, the hierarchy of macros will be traversed until an existing macro is found. % % \begin{macro}{\minted@def@optcl} % % Define a generic option that will be passed to the command line. Options are given in a |{key}{value}| format that is transformed into |key=value| and then passed to |pygmentize|. This allows |value| to be easily stored in a separate macro for later access. This is useful, for example, in separately accessing the value of |encoding| for performing |autogobble|. % % If a |key| option is specified without |=value|, the default is assumed. Options are automatically created at all levels. % % Options are added to the option lists in such a way that they will be detokenized. This is necessary since they will ultimately be used in |\write18|. % \begin{macrocode} \newcommand{\minted@addto@optlistcl}[2]{% \expandafter\def\expandafter#1\expandafter{#1% \detokenize{#2}\space}} \newcommand{\minted@addto@optlistcl@lang}[2]{% \expandafter\let\expandafter\minted@tmp\csname #1\endcsname \expandafter\def\expandafter\minted@tmp\expandafter{\minted@tmp% \detokenize{#2}\space}% \expandafter\let\csname #1\endcsname\minted@tmp} \newcommand{\minted@def@optcl}[4][]{% \ifthenelse{\equal{#1}{}}% {\define@key{minted@opt@g}{#2}{% \minted@addto@optlistcl{\minted@optlistcl@g}{#3=#4}% \@namedef{minted@opt@g:#2}{#4}}% \define@key{minted@opt@g@i}{#2}{% \minted@addto@optlistcl{\minted@optlistcl@g@i}{#3=#4}% \@namedef{minted@opt@g@i:#2}{#4}}% \define@key{minted@opt@lang}{#2}{% \minted@addto@optlistcl@lang{minted@optlistcl@lang\minted@lang}{#3=#4}% \@namedef{minted@opt@lang\minted@lang:#2}{#4}}% \define@key{minted@opt@lang@i}{#2}{% \minted@addto@optlistcl@lang{% minted@optlistcl@lang\minted@lang @i}{#3=#4}% \@namedef{minted@opt@lang\minted@lang @i:#2}{#4}}% \define@key{minted@opt@cmd}{#2}{% \minted@addto@optlistcl{\minted@optlistcl@cmd}{#3=#4}% \@namedef{minted@opt@cmd:#2}{#4}}}% {\define@key{minted@opt@g}{#2}[#1]{% \minted@addto@optlistcl{\minted@optlistcl@g}{#3=#4}% \@namedef{minted@opt@g:#2}{#4}}% \define@key{minted@opt@g@i}{#2}[#1]{% \minted@addto@optlistcl{\minted@optlistcl@g@i}{#3=#4}% \@namedef{minted@opt@g@i:#2}{#4}}% \define@key{minted@opt@lang}{#2}[#1]{% \minted@addto@optlistcl@lang{minted@optlistcl@lang\minted@lang}{#3=#4}% \@namedef{minted@opt@lang\minted@lang:#2}{#4}}% \define@key{minted@opt@lang@i}{#2}[#1]{% \minted@addto@optlistcl@lang{% minted@optlistcl@lang\minted@lang @i}{#3=#4}% \@namedef{minted@opt@lang\minted@lang @i:#2}{#4}}% \define@key{minted@opt@cmd}{#2}[#1]{% \minted@addto@optlistcl{\minted@optlistcl@cmd}{#3=#4}% \@namedef{minted@opt@cmd:#2}{#4}}}% } % \end{macrocode} % \end{macro} % % This covers the typical options that must be passed to Pygments. But some, particularly |escapeinside|, need more work. Since their arguments may contain escaped characters, expansion rather than detokenization is needed. Getting expansion to work as desired in a |\write18| context requires the redefinition of some characters % % \begin{macro}{\minted@escchars} % We need to define versions of common escaped characters that will work correctly under expansion for use in |\write18|. % \begin{macrocode} \edef\minted@hashchar{\string#} \edef\minted@dollarchar{\string$} \edef\minted@ampchar{\string&} \edef\minted@underscorechar{\string_} \edef\minted@tildechar{\string~} \edef\minted@leftsquarebracket{\string[} \edef\minted@rightsquarebracket{\string]} \newcommand{\minted@escchars}{% \let\#\minted@hashchar \let\%\@percentchar \let\{\@charlb \let\}\@charrb \let\$\minted@dollarchar \let\&\minted@ampchar \let\_\minted@underscorechar \let\\\@backslashchar \let~\minted@tildechar \let\~\minted@tildechar \let\[\minted@leftsquarebracket \let\]\minted@rightsquarebracket } %$ <- highlighting % \end{macrocode} % \end{macro} % % % \begin{macro}{\minted@def@optcl@e} % Now to define options that are expanded. % \begin{macrocode} \newcommand{\minted@addto@optlistcl@e}[2]{% \begingroup \minted@escchars \xdef\minted@xtmp{#2}% \endgroup \expandafter\minted@addto@optlistcl@e@i\expandafter{\minted@xtmp}{#1}} \def\minted@addto@optlistcl@e@i#1#2{% \expandafter\def\expandafter#2\expandafter{#2#1\space}} \newcommand{\minted@addto@optlistcl@lang@e}[2]{% \begingroup \minted@escchars \xdef\minted@xtmp{#2}% \endgroup \expandafter\minted@addto@optlistcl@lang@e@i\expandafter{\minted@xtmp}{#1}} \def\minted@addto@optlistcl@lang@e@i#1#2{% \expandafter\let\expandafter\minted@tmp\csname #2\endcsname \expandafter\def\expandafter\minted@tmp\expandafter{\minted@tmp#1\space}% \expandafter\let\csname #2\endcsname\minted@tmp} \newcommand{\minted@def@optcl@e}[4][]{% \ifthenelse{\equal{#1}{}}% {\define@key{minted@opt@g}{#2}{% \minted@addto@optlistcl@e{\minted@optlistcl@g}{#3=#4}% \@namedef{minted@opt@g:#2}{#4}}% \define@key{minted@opt@g@i}{#2}{% \minted@addto@optlistcl@e{\minted@optlistcl@g@i}{#3=#4}% \@namedef{minted@opt@g@i:#2}{#4}}% \define@key{minted@opt@lang}{#2}{% \minted@addto@optlistcl@lang@e{minted@optlistcl@lang\minted@lang}{#3=#4}% \@namedef{minted@opt@lang\minted@lang:#2}{#4}}% \define@key{minted@opt@lang@i}{#2}{% \minted@addto@optlistcl@lang@e{% minted@optlistcl@lang\minted@lang @i}{#3=#4}% \@namedef{minted@opt@lang\minted@lang @i:#2}{#4}}% \define@key{minted@opt@cmd}{#2}{% \minted@addto@optlistcl@e{\minted@optlistcl@cmd}{#3=#4}% \@namedef{minted@opt@cmd:#2}{#4}}}% {\define@key{minted@opt@g}{#2}[#1]{% \minted@addto@optlistcl@e{\minted@optlistcl@g}{#3=#4}% \@namedef{minted@opt@g:#2}{#4}}% \define@key{minted@opt@g@i}{#2}[#1]{% \minted@addto@optlistcl@e{\minted@optlistcl@g@i}{#3=#4}% \@namedef{minted@opt@g@i:#2}{#4}}% \define@key{minted@opt@lang}{#2}[#1]{% \minted@addto@optlistcl@lang@e{minted@optlistcl@lang\minted@lang}{#3=#4}% \@namedef{minted@opt@lang\minted@lang:#2}{#4}}% \define@key{minted@opt@lang@i}{#2}[#1]{% \minted@addto@optlistcl@lang@e{% minted@optlistcl@lang\minted@lang @i}{#3=#4}% \@namedef{minted@opt@lang\minted@lang @i:#2}{#4}}% \define@key{minted@opt@cmd}{#2}[#1]{% \minted@addto@optlistcl@e{\minted@optlistcl@cmd}{#3=#4}% \@namedef{minted@opt@cmd:#2}{#4}}}% } % \end{macrocode} % \end{macro} % % % \begin{macro}{\minted@def@optcl@switch} % % Define a switch or boolean option that is passed to Pygments, which is |true| when no value is specified. % \begin{macrocode} \newcommand{\minted@def@optcl@switch}[2]{% \define@booleankey{minted@opt@g}{#1}% {\minted@addto@optlistcl{\minted@optlistcl@g}{#2=True}% \@namedef{minted@opt@g:#1}{true}} {\minted@addto@optlistcl{\minted@optlistcl@g}{#2=False}% \@namedef{minted@opt@g:#1}{false}} \define@booleankey{minted@opt@g@i}{#1}% {\minted@addto@optlistcl{\minted@optlistcl@g@i}{#2=True}% \@namedef{minted@opt@g@i:#1}{true}} {\minted@addto@optlistcl{\minted@optlistcl@g@i}{#2=False}% \@namedef{minted@opt@g@i:#1}{false}} \define@booleankey{minted@opt@lang}{#1}% {\minted@addto@optlistcl@lang{minted@optlistcl@lang\minted@lang}{#2=True}% \@namedef{minted@opt@lang\minted@lang:#1}{true}} {\minted@addto@optlistcl@lang{minted@optlistcl@lang\minted@lang}{#2=False}% \@namedef{minted@opt@lang\minted@lang:#1}{false}} \define@booleankey{minted@opt@lang@i}{#1}% {\minted@addto@optlistcl@lang{minted@optlistcl@lang\minted@lang @i}{#2=True}% \@namedef{minted@opt@lang\minted@lang @i:#1}{true}} {\minted@addto@optlistcl@lang{minted@optlistcl@lang\minted@lang @i}{#2=False}% \@namedef{minted@opt@lang\minted@lang @i:#1}{false}} \define@booleankey{minted@opt@cmd}{#1}% {\minted@addto@optlistcl{\minted@optlistcl@cmd}{#2=True}% \@namedef{minted@opt@cmd:#1}{true}} {\minted@addto@optlistcl{\minted@optlistcl@cmd}{#2=False}% \@namedef{minted@opt@cmd:#1}{false}} } % \end{macrocode} % \end{macro} % % Now that all the machinery for Pygments options is in place, we can move on to \pkg{fancyvrb} options. % % \begin{macro}{\minted@def@optfv} % % Define \pkg{fancyvrb} options. % \begin{macrocode} \newcommand{\minted@def@optfv}[1]{% \define@key{minted@opt@g}{#1}{% \expandafter\def\expandafter\minted@optlistfv@g\expandafter{% \minted@optlistfv@g#1=##1,}% \@namedef{minted@opt@g:#1}{##1}} \define@key{minted@opt@g@i}{#1}{% \expandafter\def\expandafter\minted@optlistfv@g@i\expandafter{% \minted@optlistfv@g@i#1=##1,}% \@namedef{minted@opt@g@i:#1}{##1}} \define@key{minted@opt@lang}{#1}{% \expandafter\let\expandafter\minted@tmp% \csname minted@optlistfv@lang\minted@lang\endcsname \expandafter\def\expandafter\minted@tmp\expandafter{% \minted@tmp#1=##1,}% \expandafter\let\csname minted@optlistfv@lang\minted@lang\endcsname% \minted@tmp \@namedef{minted@opt@lang\minted@lang:#1}{##1}} \define@key{minted@opt@lang@i}{#1}{% \expandafter\let\expandafter\minted@tmp% \csname minted@optlistfv@lang\minted@lang @i\endcsname \expandafter\def\expandafter\minted@tmp\expandafter{% \minted@tmp#1=##1,}% \expandafter\let\csname minted@optlistfv@lang\minted@lang @i\endcsname% \minted@tmp \@namedef{minted@opt@lang\minted@lang @i:#1}{##1}} \define@key{minted@opt@cmd}{#1}{% \expandafter\def\expandafter\minted@optlistfv@cmd\expandafter{% \minted@optlistfv@cmd#1=##1,}% \@namedef{minted@opt@cmd:#1}{##1}} } % \end{macrocode} % \end{macro} % % \begin{macro}{\minted@def@optfv@switch} % % Define \pkg{fancyvrb} boolean options. % \begin{macrocode} \newcommand{\minted@def@optfv@switch}[1]{% \define@booleankey{minted@opt@g}{#1}% {\expandafter\def\expandafter\minted@optlistfv@g\expandafter{% \minted@optlistfv@g#1=true,}% \@namedef{minted@opt@g:#1}{true}}% {\expandafter\def\expandafter\minted@optlistfv@g\expandafter{% \minted@optlistfv@g#1=false,}% \@namedef{minted@opt@g:#1}{false}}% \define@booleankey{minted@opt@g@i}{#1}% {\expandafter\def\expandafter\minted@optlistfv@g@i\expandafter{% \minted@optlistfv@g@i#1=true,}% \@namedef{minted@opt@g@i:#1}{true}}% {\expandafter\def\expandafter\minted@optlistfv@g@i\expandafter{% \minted@optlistfv@g@i#1=false,}% \@namedef{minted@opt@g@i:#1}{false}}% \define@booleankey{minted@opt@lang}{#1}% {\expandafter\let\expandafter\minted@tmp% \csname minted@optlistfv@lang\minted@lang\endcsname \expandafter\def\expandafter\minted@tmp\expandafter{% \minted@tmp#1=true,}% \expandafter\let\csname minted@optlistfv@lang\minted@lang\endcsname% \minted@tmp \@namedef{minted@opt@lang\minted@lang:#1}{true}}% {\expandafter\let\expandafter\minted@tmp% \csname minted@optlistfv@lang\minted@lang\endcsname \expandafter\def\expandafter\minted@tmp\expandafter{% \minted@tmp#1=false,}% \expandafter\let\csname minted@optlistfv@lang\minted@lang\endcsname% \minted@tmp \@namedef{minted@opt@lang\minted@lang:#1}{false}}% \define@booleankey{minted@opt@lang@i}{#1}% {\expandafter\let\expandafter\minted@tmp% \csname minted@optlistfv@lang\minted@lang @i\endcsname \expandafter\def\expandafter\minted@tmp\expandafter{% \minted@tmp#1=true,}% \expandafter\let\csname minted@optlistfv@lang\minted@lang @i\endcsname% \minted@tmp \@namedef{minted@opt@lang\minted@lang @i:#1}{true}}% {\expandafter\let\expandafter\minted@tmp% \csname minted@optlistfv@lang\minted@lang @i\endcsname \expandafter\def\expandafter\minted@tmp\expandafter{% \minted@tmp#1=false,}% \expandafter\let\csname minted@optlistfv@lang\minted@lang @i\endcsname% \minted@tmp \@namedef{minted@opt@lang\minted@lang @i:#1}{false}}% \define@booleankey{minted@opt@cmd}{#1}% {\expandafter\def\expandafter\minted@optlistfv@cmd\expandafter{% \minted@optlistfv@cmd#1=true,}% \@namedef{minted@opt@cmd:#1}{true}}% {\expandafter\def\expandafter\minted@optlistfv@cmd\expandafter{% \minted@optlistfv@cmd#1=false,}% \@namedef{minted@opt@cmd:#1}{false}}% } % \end{macrocode} % \end{macro} % % \begin{macro}{minted@isinline} % In resolving value precedence when actually using values, we need a way to determine whether we are in an inline context. This is accomplished via a boolean that is set at the beginning of inline commands. % \begin{macrocode} \newboolean{minted@isinline} % \end{macrocode} % \end{macro} % % \begin{macro}{\minted@fvset} % We will need a way to actually use the lists of stored \pkg{fancyvrb} options later on. % \begin{macrocode} \newcommand{\minted@fvset}{% \expandafter\fvset\expandafter{\minted@optlistfv@g}% \expandafter\let\expandafter\minted@tmp% \csname minted@optlistfv@lang\minted@lang\endcsname \expandafter\fvset\expandafter{\minted@tmp}% \ifthenelse{\boolean{minted@isinline}}% {\expandafter\fvset\expandafter{\minted@optlistfv@g@i}% \expandafter\let\expandafter\minted@tmp% \csname minted@optlistfv@lang\minted@lang @i\endcsname \expandafter\fvset\expandafter{\minted@tmp}}% {}% \expandafter\fvset\expandafter{\minted@optlistfv@cmd}% } % \end{macrocode} % \end{macro} % % % We need a way to define \pkg{minted}-specific options at multiple levels of hierarchy, as well as a way to retrieve these options. As with previous types of options, values are stored in macros of the form |\minted@opt@|\meta{level}|:|\meta{key}, since they are not meant to be accessed directly. % % The order of precedence is |cmd|, |lang@i|, |g@i|, |lang|, |g|. A value specified at the command or environment level should override other settings. In its absence, a value specified for an inline command should override other settings, if we are indeed in an inline context. Otherwise, language settings take precedence over global settings. % % Before actually creating the option-definition macro, we need a few helper macros. % % % \begin{macro}{\minted@def@opt} % Finally, on to the actual option definitions for \pkg{minted}-specific options. % % Usage: |\minted@def@opt[|\meta{initial global value}|]{|\meta{key name}|}| % \begin{macrocode} \newcommand{\minted@def@opt}[2][]{% \define@key{minted@opt@g}{#2}{% \@namedef{minted@opt@g:#2}{##1}} \define@key{minted@opt@g@i}{#2}{% \@namedef{minted@opt@g@i:#2}{##1}} \define@key{minted@opt@lang}{#2}{% \@namedef{minted@opt@lang\minted@lang:#2}{##1}} \define@key{minted@opt@lang@i}{#2}{% \@namedef{minted@opt@lang\minted@lang @i:#2}{##1}} \define@key{minted@opt@cmd}{#2}{% \@namedef{minted@opt@cmd:#2}{##1}} } % \end{macrocode} % \end{macro} % % \begin{macro}{\minted@def@opt@style} % Define an option for styles. These are defined independently because styles need slightly different handling. It is conventient to create style macros when styles are set. Otherwise, it would be necessary to check for the existence of style macros at the beginning of every command or environment. % \begin{macrocode} \newcommand{\minted@def@opt@style}{% \define@key{minted@opt@g}{style}{% \minted@checkstyle{##1}% \@namedef{minted@opt@g:style}{##1}}% \define@key{minted@opt@g@i}{style}{% \minted@checkstyle{##1}% \@namedef{minted@opt@g@i:style}{##1}}% \define@key{minted@opt@lang}{style}{% \minted@checkstyle{##1}% \@namedef{minted@opt@lang\minted@lang:style}{##1}}% \define@key{minted@opt@lang@i}{style}{% \minted@checkstyle{##1}% \@namedef{minted@opt@lang\minted@lang @i:style}{##1}}% \define@key{minted@opt@cmd}{style}{% \minted@checkstyle{##1}% \@namedef{minted@opt@cmd:style}{##1}}% } % \end{macrocode} % \end{macro} % % % \begin{macro}{\minted@checkstyle} % Make sure that style macros exist. % % We have to do some tricks with |\endlinechar| to prevent |\input| from inserting unwanted whitespace. That is primarily for inline commands, where it would introduce a line break. There is also the very unorthodox |\let\def\gdef| to make sure that macros are defined globally. % % If a style is not given, then revert to the |default| style, but create macros with prefix |PYG|, and create |default-pyg-prefix.pygstyle| if caching is on. This allows a graceful fallback in the event that style is empty. It is also purposefully used to create a complete set of macros with prefix |PYG|, so that the symbol macros may be used, as described next. % % The typical style macros created by |\minted@checkstyle|, which are of the form |\PYG