How do you change the code example font size in LaTeX PDF output with sphinx?

a 夏天 提交于 2019-12-22 04:32:34

问题


I find the default code example font in the PDF generated by sphinx to be far too large.

I've tried getting my hands dirty in the generated .tex file inserting font size commands like \tiny above the code blocks, but it just makes the line above the code block tiny, not the code block itself.

I'm not sure what else to do - I'm an absolute beginner with LaTeX.


回答1:


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




回答2:


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?



回答3:


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]%
}


来源:https://stackoverflow.com/questions/9899283/how-do-you-change-the-code-example-font-size-in-latex-pdf-output-with-sphinx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!