How do you change the code example font size in LaTeX PDF output with Sphinx?
I worked it out. Pygments uses a \begin{Verbatim}
block to denote code snippets, which uses the fancyvrb
package. The documentation I found (warning: PDF) mentions a formatcom
option for the verbatim block.
Pygments' latex writer source indicates an instance variable, verboptions
, is stapled to the end of each verbatim block and Sphinx' latex bridge lets you replace the LatexFormatter
.
At the top of my conf.py
file, I added the following:
from sphinx.highlighting import PygmentsBridge
from pygments.formatters.latex import LatexFormatter
class CustomLatexFormatter(LatexFormatter):
def __init__(self, **options):
super(CustomLatexFormatter, self).__init__(**options)
self.verboptions = r"formatcom=\footnotesize"
PygmentsBridge.latex_formatter = CustomLatexFormatter
\footnotesize
was my preference, but a list of sizes is available here
To change Latex Output options in sphinx, set the relevant latex_elements
key in the build configuration file, documentation on this is located here.
To change the font size for all fonts use pointsize
.
E.g.
latex_elements = {
'pointsize':'10pt'
}
To change other Latex settings that are listed in the documetntation use preamble
or use a custom document class in latex_documents
.
E.g.
mypreamble='''customlatexstuffgoeshere
'''
latex_elements = {
'papersize':'letterpaper',
'pointsize':'11pt',
'preamble':mypreamble
}
Reading the Sphinx sourcecode by default the code in LatexWriter sets code snippets to the \code
latex primitive.
So what you want to do is replace the \code
with a suitable replacement.
This is done by including a Latex command like \newcommand{\code}[1]{\texttt{\tiny{#1}}}
either as part of the preamble or as part of a custom document class for sphinx that gets set in latex_documents
as the documentclass key. An example sphinx document class is avaliable here.
Other than just making it smaller with \tiny
you can modify the latex_documents
document class or the latex_elements
preamble to use the Latex package listings for more fancy code formatting like in the StackOverflow question here.
The package stuff from the linked post would go as a custom document class and the redefinition similar to \newcommand{\code}[1]{\begin{lstlisting} #1 \end{lstlisting}}
would be part of the preamble.
Alternatively you could write a sphinx extension that extends the default latex writer with a custom latex writer of your choosing though that is significantly more effort.
Other relevant StackOverflow questions include
- Creating Math Macros with Sphinx
- How do I disable colors in LaTeX output generated from sphinx?
- sphinx customization of latexpdf output?
You can add a modified Verbatim command into your PREAMBLE (Note in this case the font size is changed to tiny)
\renewcommand{\Verbatim}[1][1]{%
% list starts new par, but we don't want it to be set apart vertically
\bgroup\parskip=0pt%
\smallskip%
% The list environement is needed to control perfectly the vertical
% space.
\list{}{%
\setlength\parskip{0pt}%
\setlength\itemsep{0ex}%
\setlength\topsep{0ex}%
\setlength\partopsep{0pt}%
\setlength\leftmargin{10pt}%
}%
\item\MakeFramed {\FrameRestore}%
\tiny % <---------------- To be changed!
\OriginalVerbatim[#1]%
}