LaTeX customization¶
For details of the LaTeX/PDF builder command line invocation, refer to
LaTeXBuilder.
Basic customization¶
The latex target does not benefit from prepared themes.
Basic customization is obtained via usage of the Options for LaTeX output. For example:
# inside conf.py
latex_engine = 'xelatex'
latex_elements = {
'fontpkg': r'''
\setmainfont{DejaVu Serif}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans Mono}
''',
'preamble': r'''
\usepackage[titles]{tocloft}
\cftsetpnumwidth {1.25cm}\cftsetrmarg{1.5cm}
\setlength{\cftchapnumwidth}{0.75cm}
\setlength{\cftsecindent}{\cftchapnumwidth}
\setlength{\cftsecnumwidth}{1.25cm}
''',
'fncychap': r'\usepackage[Bjornstrup]{fncychap}',
'printindex': r'\footnotesize\raggedright\printindex',
}
latex_show_urls = 'footnote'
If the size of the 'preamble' contents becomes inconvenient, one may move
all needed macros into some file mystyle.tex.txt of the project source
repertory, and get LaTeX to import it at run time:
'preamble': r'\input{mystyle.tex.txt}',
# or, if the \ProvidesPackage LaTeX macro is used in a file mystyle.sty
'preamble': r'\usepackage{mystyle}',
It is then needed to set appropriately latex_additional_files, for
example:
latex_additional_files = ["mystyle.sty"]
The LaTeX style file options¶
Additional customization is possible via LaTeX options of the Sphinx style file.
The sphinxsetup interface¶
The 'sphinxsetup' key of latex_elements provides a convenient
interface:
latex_elements = {
'sphinxsetup': 'key1=value1, key2=value2, ...',
}
- some values may be LaTeX macros, then the backslashes must be Python-escaped, or the whole must be a Python raw string,
- LaTeX boolean keys require lowercase
trueorfalsevalues, - spaces around the commas and equal signs are ignored, spaces inside LaTeX macros may be significant.
If non-empty, it will be passed as argument to the \sphinxsetup macro
inside the document preamble, like this:
\usepackage{sphinx}
\sphinxsetup{key1=value1, key2=value2,...}
New in version 1.5.
Hint
It is possible to insert further uses of the \sphinxsetup LaTeX macro
directly into the body of the document, via the help of the raw
directive. Here is how this present chapter in PDF is styled:
.. raw:: latex
\begingroup
\sphinxsetup{%
verbatimwithframe=false,
VerbatimColor={named}{OldLace},
TitleColor={named}{DarkGoldenrod},
hintBorderColor={named}{LightCoral},
attentionborder=3pt,
attentionBorderColor={named}{Crimson},
attentionBgColor={named}{FloralWhite},
noteborder=2pt,
noteBorderColor={named}{Olive},
cautionborder=3pt,
cautionBorderColor={named}{Cyan},
cautionBgColor={named}{LightCyan}}
at the start of the chapter and:
.. raw:: latex
\endgroup
at its end.
The colors used in the above are provided by the svgnames option of the
“xcolor” package:
latex_elements = {
'passoptionstopackages': r'\PassOptionsToPackage{svgnames}{xcolor}',
}
The available styling options¶
hmargin, vmarginThe dimensions of the horizontal (resp. vertical) margins, passed as
hmargin(resp.vmargin) option to thegeometrypackage. The default is1in, which is equivalent to{1in,1in}. Example:'sphinxsetup': 'hmargin={2in,1.5in}, vmargin={1.5in,2in}, marginpar=1in',
Japanese documents currently accept only the one-dimension format for these parameters. The
geometrypackage is then passed suitable options to get the text width set to an exact multiple of the zenkaku width, and the text height set to an integer multiple of the baselineskip, with the closest fit for the margins.Hint
For Japanese
'manual'docclass with pointsize11ptor12pt, use thenomagextra document class option (cf.'extraclassoptions'key oflatex_elements) or so-called TeX “true” units:'sphinxsetup': 'hmargin=1.5truein, vmargin=1.5truein, marginpar=5zw',
New in version 1.5.3.
marginparThe
\marginparwidthLaTeX dimension, defaults to0.5in. For Japanese documents, the value is modified to be the closest integer multiple of the zenkaku width.New in version 1.5.3.
verbatimwithframe- default
true. Boolean to specify ifcode-blocks and literal includes are framed. Setting it tofalsedoes not deactivate use of package “framed”, because it is still in use for the optional background colour. verbatimwrapslines- default
true. Tells whether long lines incode-block’s contents should wrap. literalblockcapposdefault
tfor “top”. Decides the caption position. Alternative isb(“bottom”).New in version 1.7.
verbatimhintsturnoverdefault
true. Iftrue, code-blocks display “continued on next page”, “continued from previous page” hints in case of pagebreaks.New in version 1.6.3.
Changed in version 1.7: the default changed from
falsetotrue.verbatimcontinuedalign,verbatimcontinuesaligndefault
c. Horizontal position relative to the framed contents: eitherl(left aligned),r(right aligned) orc(centered).New in version 1.7.
parsedliteralwrapsdefault
true. Tells whether long lines in parsed-literal’s contents should wrap.New in version 1.5.2: set this option value to
falseto recover former behaviour.inlineliteralwrapsdefault
true. Allows linebreaks inside inline literals: but extra potential break-points (additionally to those allowed by LaTeX at spaces or for hyphenation) are currently inserted only after the characters. , ; ? ! /. Due to TeX internals, white space in the line will be stretched (or shrunk) in order to accomodate the linebreak.New in version 1.5: set this option value to
falseto recover former behaviour.verbatimvisiblespace- default
\textcolor{red}{\textvisiblespace}. When a long code line is split, the last space character from the source code line right before the linebreak location is typeset using this. verbatimcontinuedA LaTeX macro inserted at start of continuation code lines. Its (complicated…) default typesets a small red hook pointing to the right:
\makebox[2\fontcharwd\font`\x][r]{\textcolor{red}{\tiny$\hookrightarrow$}}
Changed in version 1.5: The breaking of long code lines was added at 1.4.2. The default definition of the continuation symbol was changed at 1.5 to accomodate various font sizes (e.g. code-blocks can be in footnotes).
TitleColor- default
{rgb}{0.126,0.263,0.361}. The colour for titles (as configured via use of package “titlesec”.)
Warning
Colours set via 'sphinxsetup' must obey the syntax of the
argument of the color/xcolor packages \definecolor command.
InnerLinkColor- default
{rgb}{0.208,0.374,0.486}. A colour passed tohyperrefas value oflinkcolorandcitecolor. OuterLinkColor- default
{rgb}{0.216,0.439,0.388}. A colour passed tohyperrefas value offilecolor,menucolor, andurlcolor. VerbatimColor- default
{rgb}{1,1,1}. The background colour forcode-blocks. The default is white. VerbatimBorderColor- default
{rgb}{0,0,0}. The frame color, defaults to black. VerbatimHighlightColordefault
{rgb}{0.878,1,1}. The color for highlighted lines.New in version 1.6.6.
Note
Starting with this colour key, and for all others coming next, the actual names declared to “color” or “xcolor” are prefixed with “sphinx”.
verbatimsep- default
\fboxsep. The separation between code lines and the frame. verbatimborder- default
\fboxrule. The width of the frame aroundcode-blocks. shadowsep- default
5pt. The separation between contents and frame for contents and topic boxes. shadowsize- default
4pt. The width of the lateral “shadow” to the right. shadowrule- default
\fboxrule. The width of the frame around topic boxes. noteBorderColor,hintBorderColor,importantBorderColor,tipBorderColor- default
{rgb}{0,0,0}(black). The colour for the two horizontal rules used by Sphinx in LaTeX for styling a note type admonition. noteborder,hintborder,importantborder,tipborder- default
0.5pt. The width of the two horizontal rules.
warningBorderColor,cautionBorderColor,attentionBorderColor,dangerBorderColor,errorBorderColor- default
{rgb}{0,0,0}(black). The colour for the admonition frame.
warningBgColor,cautionBgColor,attentionBgColor,dangerBgColor,errorBgColor- default
{rgb}{1,1,1}(white). The background colours for the respective admonitions. warningBorder,cautionBorder,attentionBorder,dangerBorder,errorBorder- default
1pt. The width of the frame. AtStartFootnote- default
\mbox{ }. LaTeX macros inserted at the start of the footnote text at bottom of page, after the footnote number. BeforeFootnotedefault
\leavevmode\unskip. LaTeX macros inserted before the footnote mark. The default removes possible space before it (else, TeX could insert a linebreak there).New in version 1.5.
HeaderFamily- default
\sffamily\bfseries. Sets the font used by headings.
LaTeX macros and environments¶
Here are some macros from the package file sphinx.sty and class files
sphinxhowto.cls, sphinxmanual.cls, which have public names
thus allowing redefinitions. Check the respective files for the defaults.
Macros¶
text styling commands
\sphinx<foo>with<foo>being one ofstrong,bfcode,email,tablecontinued,titleref,menuselection,accelerator,crossref,termref,optional.New in version 1.4.5: Use of
\sphinxprefixed macro names to limit possibilities of conflict with LaTeX packages.more text styling:
\sphinxstyle<bar>with<bar>one ofindexentry,indexextra,indexpageref,topictitle,sidebartitle,othertitle,sidebarsubtitle,theadfamily,emphasis,literalemphasis,strong,literalstrong,abbreviation,literalintitle,codecontinued,codecontinuesNew in version 1.5: these macros were formerly hard-coded as non customizable
\texttt,\emph, etc…New in version 1.6:
\sphinxstyletheadfamilywhich defaults to\sffamilyand allows multiple paragraphs in header cells of tables.New in version 1.6.3:
\sphinxstylecodecontinuedand\sphinxstylecodecontinues.by default the Sphinx style file
sphinx.styexecutes the command\fvset{fontsize=\small}as part of its configuration offancyvrb.sty. This may be overriden for example via\fvset{fontsize=auto}which will let code listings use the ambient font size. Refer tofancyvrb.sty’s documentation for further keys.New in version 1.5.
the table of contents is typeset via
\sphinxtableofcontentswhich is a wrapper (whose definition can be found insphinxhowto.clsor insphinxmanual.cls) of standard\tableofcontents.Changed in version 1.5: formerly, the meaning of
\tableofcontentswas modified by Sphinx.the
\maketitlecommand is redefined by the class filessphinxmanual.clsandsphinxhowto.cls.
Environments¶
a figure may have an optional legend with arbitrary body elements: they are rendered in a
sphinxlegendenvironment. The default definition issues\small, and ends with\par.New in version 1.5.6: formerly, the
\smallwas hardcoded in LaTeX writer and the ending\parwas lacking.for each admonition type
<foo>, the used environment is namedsphinx<foo>. They may be\renewenvironment‘d individually, and must then be defined with one argument (it is the heading of the notice, for exampleWarning:for warning directive, if English is the document language). Their default definitions use either the sphinxheavybox (for the first listed directives) or the sphinxlightbox environments, configured to use the parameters (colours, border thickness) specific to each type, which can be set via'sphinxsetup'string.Changed in version 1.5: use of public environment names, separate customizability of the parameters, such as
noteBorderColor,noteborder,warningBgColor,warningBorderColor,warningborder, …the contents directive (with
:local:option) and the topic directive are implemented by environmentsphinxShadowBox.New in version 1.4.2: former code refactored into an environment allowing page breaks.
Changed in version 1.5: options
shadowsep,shadowsize,shadowrule.the literal blocks (via
::orcode-block), are implemented usingsphinxVerbatimenvironment which is a wrapper ofVerbatimenvironment from packagefancyvrb.sty. It adds the handling of the top caption and the wrapping of long lines, and a frame which allows pagebreaks. Inside tables the used environment issphinxVerbatimintable(it does not draw a frame, but allows a caption).Changed in version 1.5:
Verbatimkeeps exact same meaning as infancyvrb.sty(also under the nameOriginalVerbatim);sphinxVerbatimintableis used inside tables.New in version 1.5: options
verbatimwithframe,verbatimwrapslines,verbatimsep,verbatimborder.New in version 1.6.6: support for
:emphasize-lines:optionNew in version 1.6.6: easier customizability of the formatting via exposed to user LaTeX macros such as
\sphinxVerbatimHighlightLine.the bibliography uses
sphinxthebibliographyand the Python Module index as well as the general index both usesphinxtheindex; these environments are wrappers of thethebibliographyand respectivelytheindexenvironments as provided by the document class (or packages).Changed in version 1.5: formerly, the original environments were modified by Sphinx.
Miscellany¶
the section, subsection, … headings are set using titlesec’s
\titleformatcommand.for the
'manual'docclass, the chapter headings can be customized using fncychap’s commands\ChNameVar,\ChNumVar,\ChTitleVar. Filesphinx.styhas custom re-definitions in case of fncychap optionBjarne.Changed in version 1.5: formerly, use of fncychap with other styles than
Bjarnewas dysfunctional.
Hint
As an experimental feature, Sphinx can use user-defined template file for
LaTeX source if you have a file named _templates/latex.tex_t in your
project.
New in version 1.5: currently all template variables are unstable and undocumented.
Additional files longtable.tex_t, tabulary.tex_t and
tabular.tex_t can be added to _templates/ to configure some aspects
of table rendering (such as the caption position).
New in version 1.6: currently all template variables are unstable and undocumented.