LaTeX のカスタマイズ

The latex target does not benefit from prepared themes.

The LaTeX出力のオプション, and particularly among them the latex_elements variable provides much of the interface for customization.

For some details of the LaTeX/PDF builder command line invocation, refer to LaTeXBuilder.


Keep in mind that backslashes must be doubled in Python string literals to avoid interpretation as escape sequences, or use raw strings (as is done here).

# inside
latex_engine = 'xelatex'
latex_elements = {
    'fontpkg': r'''
\setmainfont{DejaVu Serif}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans Mono}
    'preamble': r'''
\cftsetpnumwidth {1.25cm}\cftsetrmarg{1.5cm}
    'fncychap': r'\usepackage[Bjornstrup]{fncychap}',
    'printindex': r'\footnotesize\raggedright\printindex',
latex_show_urls = 'footnote'

The latex_elements configuration setting

A dictionary that contains LaTeX snippets overriding those Sphinx usually puts into the generated .tex files. Its 'sphinxsetup' key is described separately.

  • オーバーライドできるキーには、次のようなものがあります:


    document classの用紙サイズのオプションです。 'a4paper''letterpaper' が指定できます。デフォルトは 'letterpaper' です。


    document classのポイントサイズのオプションです。 '10pt''11pt', '12pt' が指定できます。デフォルトは '10pt' です。


    the value of the px when used in image attributes width and height. The default value is '0.75bp' which achieves 96px=1in (in TeX 1in = 72bp = 72.27pt.) To obtain for example 100px=1in use '0.01in' or '0.7227pt' (the latter leads to TeX computing a more precise value, due to the smaller unit used in the specification); for 72px=1in, simply use '1bp'; for 90px=1in, use '0.8bp' or '0.803pt'.

    バージョン 1.5 で追加.


    A string which will be positioned early in the preamble, designed to contain \\PassOptionsToPackage{options}{foo} commands. Default empty.

    バージョン 1.4 で追加.


    "babel" package inclusion, default '\\usepackage{babel}' (the suitable document language string is passed as class option, and english is used if no language.) For Japanese documents, the default is the empty string.

    With XeLaTeX and LuaLaTeX, Sphinx configures the LaTeX document to use polyglossia, but one should be aware that current babel has improved its support for Unicode engines in recent years and for some languages it may make sense to prefer babel over polyglossia.


    After modifiying a core LaTeX key like this one, clean up the LaTeX build repertory before next PDF build, else left-over auxiliary files are likely to break the build.

    バージョン 1.5 で変更: For latex_engine set to 'xelatex', the default is '\\usepackage{polyglossia}\n\\setmainlanguage{<language>}'.

    バージョン 1.6 で変更: 'lualatex' uses same default setting as 'xelatex'

    バージョン 1.7.6 で変更: For French, xelatex and lualatex default to using babel, not polyglossia.


    Font package inclusion, the default is '\\usepackage{times}' which uses Times for text, Helvetica for sans serif and Courier for monospace.

    バージョン 1.2 で変更: language がキリル文字を使用する場合、デフォルトを '' にしました。

    バージョン 2.0 で変更: Support for individual Greek and Cyrillic letters:

    • In order to support occasional Cyrillic (физика частиц) or Greek letters (Σωματιδιακή φυσική) in a document whose language is English or a Latin European one, the default set-up is enhanced (only for 'pdflatex' engine) to do:


      but this is activated only under the condition that the 'fontenc' key is configured to load the LGR (Greek) and/or X2 (Cyrillic) pdflatex-font encodings (if the language is set to a Cyrillic language, this 'fontpkg' key must be used as "times" package has no direct support for it; then keep only LGR lines from the above, if support is needed for Greek in the text).

      The \substitutefont command is from the eponymous LaTeX package, which is loaded by Sphinx if needed (on Ubuntu xenial it is part of texlive-latex-extra which is a Sphinx requirement).

      Only if the document actually does contain Unicode Greek letters (in text) or Cyrillic letters, will the above default set-up cause additional requirements for the PDF build. On Ubuntu xenial, texlive-lang-greek, texlive-lang-cyrillic, and (with the above choice of fonts) the cm-super (or cm-super-minimal) package.

    • For 'xelatex' and 'lualatex', the default is to use the FreeFont family: this OpenType font family supports both Cyrillic and Greek scripts and is available as separate Ubuntu xenial package fonts-freefont-otf, it is not needed to install the big package texlive-fonts-extra.

    • 'platex' (Japanese documents) engine supports individual Cyrillic and Greek letters with no need of extra user set-up.


    Inclusion of the "fncychap" package (which makes fancy chapter titles), default '\\usepackage[Bjarne]{fncychap}' for English documentation (this option is slightly customized by Sphinx), '\\usepackage[Sonny]{fncychap}' for internationalized docs (because the "Bjarne" style uses numbers spelled out in English). Other "fncychap" styles you can try are "Lenny", "Glenn", "Conny", "Rejne" and "Bjornstrup". You can also set this to '' to disable fncychap.

    The default is '' for Japanese documents.


    Additional preamble content, default empty. 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"]

    Latexの図のフロート配置で、デフォルトは 'htbp' (here, top, bottom, page) です。現在のページ内部にイメージがフィットしてようがしていまいが、次ページに 'floated' されますがおそらく他のどんなテキストよりも優先されます。もしあなたがこの動作を気に入らないのであれば、フローティングを無効化し図を厳密に配置しソース内に現れるよう 'H' を使ってください。

    バージョン 1.3 で追加.


    Additional document content (right before the indices), default empty.

    バージョン 1.5 で追加.



    バージョン 1.5 で非推奨: Use 'atendofbody' key instead.

  • Keys that don't need to be overridden unless in special cases are:


    The default is the empty string. Example: 'extraclassoptions': 'openany' will allow chapters (for documents of the 'manual' type) to start on any page.

    バージョン 1.2 で追加.

    バージョン 1.6 で変更: Added this documentation.


    LaTeX allows by default at most 6 levels for nesting list and quote-like environments, with at most 4 enumerated lists, and 4 bullet lists. Setting this key for example to '10' (as a string) will allow up to 10 nested levels (of all sorts). Leaving it to the empty string means to obey the LaTeX default.


    • Using this key may prove incompatible with some LaTeX packages or special document classes which do their own list customization.

    • The key setting is silently ignored if \usepackage{enumitem} is executed inside the document preamble. Use then rather the dedicated commands of this LaTeX package.

    バージョン 1.5 で追加.


    "inputenc" package inclusion, defaults to '\\usepackage[utf8]{inputenc}' when using pdflatex. Otherwise empty.

    バージョン 1.4.3 で変更: Previously '\\usepackage[utf8]{inputenc}' was used for all compilers.


    "cmap" パッケージを挿入します。デフォルトでは '\\usepackage{cmap}' になります。

    バージョン 1.2 で追加.


    "fontenc" package inclusion, defaults to '\\usepackage[T1]{fontenc}'.

    If 'pdflatex' is the latex_engine, one can add LGR for support of Greek letters in the document, and also X2 (or T2A) for Cyrillic letters, like this:



    Prior to 2.0, Unicode Greek letters were escaped to use LaTeX math mark-up. This is not the case anymore, and the above must be used (only in case of 'pdflatex' engine) if the source contains such Unicode Greek.

    On Ubuntu xenial, packages texlive-lang-greek and cm-super (for the latter, only if the 'fontpkg' setting is left to its default) are needed for LGR to work. In place of cm-super one can install smaller cm-super-minimal, but it requires the LaTeX document to execute \usepackage[10pt]{type1ec} before loading fontenc. Thus, use this key with this extra at its start if needed.

    バージョン 1.5 で変更: Defaults to '\\usepackage{fontspec}' when latex_engine is 'xelatex'.

    バージョン 1.6 で変更: 'lualatex' uses fontspec per default like 'xelatex'.

    バージョン 2.0 で変更: 'lualatex' executes \defaultfontfeatures[\rmfamily,\sffamily]{} to disable TeX ligatures.

    バージョン 2.0 で変更: Detection of LGR, T2A, X2 to trigger support of occasional Greek or Cyrillic ('pdflatex' only, as this support is provided natively by 'platex' and only requires suitable font with 'xelatex'/'lualatex').


    The default ('pdflatex' only) is '\\usepackage{textalpha}', but only if 'fontenc' was modified by user to include LGR option. If not, the key value will be forced to be the empty string.

    This is needed for pdfLaTeX to support Unicode input of Greek letters such as φύσις. Expert users may want to load the textalpha package with its option normalize-symbols.


    Unicode Greek (but no further Unicode symbols) in math can be supported by 'pdflatex' from setting this key to r'\usepackage{textalpha,alphabeta}'. Then :math:`α` (U+03B1) will render as \(\alpha\). For wider Unicode support in math input, see the discussion of latex_engine.

    With 'platex' (Japanese), 'xelatex' or 'lualatex', this key is ignored.

    バージョン 2.0 で追加.


    "geometry" package inclusion, the default definition is:


    with an additional [dvipdfm] for Japanese documents. The Sphinx LaTeX style file executes:


    which can be customized via corresponding 'sphinxsetup' options.

    バージョン 1.5 で追加.

    バージョン 1.5.2 で変更: dvipdfm option if latex_engine is 'platex'.

    バージョン 1.5.3 で追加: The 'sphinxsetup' keys for the margins.

    バージョン 1.5.3 で変更: The location in the LaTeX file has been moved to after \usepackage{sphinx} and \sphinxsetup{..}, hence also after insertion of 'fontpkg' key. This is in order to handle the paper layout options in a special way for Japanese documents: the text width will be set to an integer multiple of the zenkaku width, and the text height to an integer multiple of the baseline. See the hmargin documentation for more.


    "hyperref" package inclusion; also loads package "hypcap" and issues \urlstyle{same}. This is done after sphinx.sty file is loaded and before executing the contents of 'preamble' key.


    Loading of packages "hyperref" and "hypcap" is mandatory.

    バージョン 1.5 で追加: Previously this was done from inside sphinx.sty.


    "maketitle" call, default '\\sphinxmaketitle'. Override if you want to generate a differently styled title page.


    If the key value is set to r'\newcommand\sphinxbackoftitlepage{<Extra material>}\sphinxmaketitle', then <Extra material> will be typeset on back of title page ('manual' docclass only).

    バージョン 1.8.3 で変更: Original \maketitle from document class is not overwritten, hence is re-usable as part of some custom setting for this key.

    バージョン 1.8.3 で追加: \sphinxbackoftitlepage optional macro. It can also be defined inside 'preamble' key rather than this one.


    value that prefixes 'release' element on title page, default 'Release'. As for title and author used in the tuples of latex_documents, it is inserted as LaTeX markup.


    "tableofcontents" call, default '\\sphinxtableofcontents' (it is a wrapper of unmodified \tableofcontents, which may itself be customized by user loaded packages.) Override if you want to generate a different table of contents or put content between the title page and the TOC.

    バージョン 1.5 で変更: Previously the meaning of \tableofcontents itself was modified by Sphinx. This created an incompatibility with dedicated packages modifying it also such as "tocloft" or "etoc".


    Commands used to display transitions, default '\n\n\\bigskip\\hrule\\bigskip\n\n'. Override if you want to display transitions differently.

    バージョン 1.2 で追加.

    バージョン 1.6 で変更: Remove unneeded {} after \\hrule.


    "printindex" call, the last thing in the file, default '\\printindex'. Override if you want to generate the index differently or append some content after the index. For example '\\footnotesize\\raggedright\\printindex' is advisable when the index is full of long entries.


    Customization of fancyvrb LaTeX package. Sphinx does by default 'fvset': '\\fvset{fontsize=\\small}', to adjust for the large character width of the monospace font, used in code-blocks. You may need to modify this if you use custom fonts.

    バージョン 1.8 で追加.

    バージョン 2.0 で変更: Due to new default font choice for 'xelatex' and 'lualatex' (FreeFont), Sphinx does \\fvset{fontsize=\\small} also with these engines (and not \\fvset{fontsize=auto}).

  • 次のようなキーは、他のオプションによって指定されるため、オーバーライドすべきではありません:

    'docclass' 'classoptions' 'title' 'release' 'author' 'makeindex'

\'sphinxsetup\' key

The 'sphinxsetup' key of latex_elements provides a LaTeX-type customization interface:

latex_elements = {
    'sphinxsetup': 'key1=value1, key2=value2, ...',

It defaults to empty. If non-empty, it will be passed as argument to the \sphinxsetup macro inside the document preamble, like this:

\sphinxsetup{key1=value1, key2=value2,...}

バージョン 1.5 で追加.


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 is styled in the PDF output:

.. raw:: latex


at the start of the chapter and:

.. raw:: latex


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}',

LaTeX boolean keys require lowercase true or false values. Spaces around the commas and equal signs are ignored, spaces inside LaTeX macros may be significant.

hmargin, vmargin

The dimensions of the horizontal (resp. vertical) margins, passed as hmargin (resp. vmargin) option to the geometry package. The default is 1in, 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 geometry package 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.


For Japanese 'manual' docclass with pointsize 11pt or 12pt, use the nomag extra document class option (cf. 'extraclassoptions' key of latex_elements) or so-called TeX "true" units:

'sphinxsetup': 'hmargin=1.5truein, vmargin=1.5truein, marginpar=5zw',

バージョン 1.5.3 で追加.


The \marginparwidth LaTeX dimension, defaults to 0.5in. For Japanese documents, the value is modified to be the closest integer multiple of the zenkaku width.

バージョン 1.5.3 で追加.


default true. Boolean to specify if code-blocks and literal includes are framed. Setting it to false does not deactivate use of package "framed", because it is still in use for the optional background colour.


default true. Tells whether long lines in code-block's contents should wrap.


default t for "top". Decides the caption position. Alternative is b ("bottom").

バージョン 1.7 で追加.


default true. If true, code-blocks display "continued on next page", "continued from previous page" hints in case of pagebreaks.

バージョン 1.6.3 で追加.

バージョン 1.7 で変更: the default changed from false to true.

verbatimcontinuedalign, verbatimcontinuesalign

default r. Horizontal position relative to the framed contents: either l (left aligned), r (right aligned) or c (centered).

バージョン 1.7 で追加.


default true. Tells whether long lines in parsed-literal's contents should wrap.

バージョン 1.5.2 で追加: set this option value to false to recover former behaviour.


default 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.

バージョン 1.5 で追加: set this option value to false to recover former behaviour.


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.


A LaTeX macro inserted at start of continuation code lines. Its (complicated...) default typesets a small red hook pointing to the right:


バージョン 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).


default {rgb}{0.126,0.263,0.361}. The colour for titles (as configured via use of package "titlesec".)


Colours set via 'sphinxsetup' must obey the syntax of the argument of the color/xcolor packages \definecolor command.


default {rgb}{0.208,0.374,0.486}. A colour passed to hyperref as value of linkcolor and citecolor.


default {rgb}{0.216,0.439,0.388}. A colour passed to hyperref as value of filecolor, menucolor, and urlcolor.


default {rgb}{1,1,1}. The background colour for code-blocks. The default is white.


default {rgb}{0,0,0}. The frame color, defaults to black.


default {rgb}{0.878,1,1}. The color for highlighted lines.

バージョン 1.6.6 で追加.


Starting with this colour key, and for all others coming next, the actual names declared to "color" or "xcolor" are prefixed with "sphinx".


default \fboxsep. The separation between code lines and the frame.


default \fboxrule. The width of the frame around code-blocks.


default 5pt. The separation between contents and frame for contents and topic boxes.


default 4pt. The width of the lateral "shadow" to the right.


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.


default \mbox{ }. LaTeX macros inserted at the start of the footnote text at bottom of page, after the footnote number.


default \leavevmode\unskip. LaTeX macros inserted before the footnote mark. The default removes possible space before it (else, TeX could insert a linebreak there).

バージョン 1.5 で追加.


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.


  • text styling commands \sphinx<foo> with <foo> being one of strong, bfcode, email, tablecontinued, titleref, menuselection, accelerator, crossref, termref, optional.

    バージョン 1.4.5 で追加: Use of \sphinx prefixed macro names to limit possibilities of conflict with LaTeX packages.

  • more text styling: \sphinxstyle<bar> with <bar> one of indexentry, indexextra, indexpageref, topictitle, sidebartitle, othertitle, sidebarsubtitle, theadfamily, emphasis, literalemphasis, strong, literalstrong, abbreviation, literalintitle, codecontinued, codecontinues

    バージョン 1.5 で追加: these macros were formerly hard-coded as non customizable \texttt, \emph, etc...

    バージョン 1.6 で追加: \sphinxstyletheadfamily which defaults to \sffamily and allows multiple paragraphs in header cells of tables.

    バージョン 1.6.3 で追加: \sphinxstylecodecontinued and \sphinxstylecodecontinues.

  • the table of contents is typeset via \sphinxtableofcontents which is a wrapper (defined differently in sphinxhowto.cls and in sphinxmanual.cls) of standard \tableofcontents. The macro \sphinxtableofcontentshook is executed during its expansion right before \tableofcontents itself.

    バージョン 1.5 で変更: formerly, the meaning of \tableofcontents was modified by Sphinx.

    バージョン 2.0 で変更: hard-coded redefinitions of \l@section and \l@subsection formerly done during loading of 'manual' docclass are now executed later via \sphinxtableofcontentshook. This macro is also executed by the 'howto' docclass, but defaults to empty with it.

  • a custom \sphinxmaketitle is defined in the class files sphinxmanual.cls and sphinxhowto.cls and is used as default setting of 'maketitle' latex_elements key.

    バージョン 1.8.3 で変更: formerly, \maketitle from LaTeX document class was modified by Sphinx.

  • for 'manual' docclass a macro \sphinxbackoftitlepage, if it is defined, gets executed at end of \sphinxmaketitle, before the final \clearpage. Use either the 'maketitle' key or the 'preamble' key of latex_elements to add a custom definition of \sphinxbackoftitlepage.

    バージョン 1.8.3 で追加.

  • the citation reference is typeset via \sphinxcite which is a wrapper of standard \cite.


  • a figure may have an optional legend with arbitrary body elements: they are rendered in a sphinxlegend environment. The default definition issues \small, and ends with \par.

    バージョン 1.5.6 で追加: formerly, the \small was hardcoded in LaTeX writer and the ending \par was lacking.

  • for each admonition type <foo>, the used environment is named sphinx<foo>. They may be \renewenvironment 'd individually, and must then be defined with one argument (it is the heading of the notice, for example Warning: 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.

    バージョン 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 environment sphinxShadowBox.

    バージョン 1.4.2 で追加: former code refactored into an environment allowing page breaks.

    バージョン 1.5 で変更: options shadowsep, shadowsize, shadowrule.

  • the literal blocks (via :: or code-block), are implemented using sphinxVerbatim environment which is a wrapper of Verbatim environment from package fancyvrb.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 is sphinxVerbatimintable (it does not draw a frame, but allows a caption).

    バージョン 1.5 で変更: Verbatim keeps exact same meaning as in fancyvrb.sty (also under the name OriginalVerbatim); sphinxVerbatimintable is used inside tables.

    バージョン 1.5 で追加: options verbatimwithframe, verbatimwrapslines, verbatimsep, verbatimborder.

    バージョン 1.6.6 で追加: support for :emphasize-lines: option

    バージョン 1.6.6 で追加: easier customizability of the formatting via exposed to user LaTeX macros such as \sphinxVerbatimHighlightLine.

  • the bibliography uses sphinxthebibliography and the Python Module index as well as the general index both use sphinxtheindex; these environments are wrappers of the thebibliography and respectively theindex environments as provided by the document class (or packages).

    バージョン 1.5 で変更: formerly, the original environments were modified by Sphinx.


  • the section, subsection, ... headings are set using titlesec's \titleformat command.

  • for the 'manual' docclass, the chapter headings can be customized using fncychap's commands \ChNameVar, \ChNumVar, \ChTitleVar. File sphinx.sty has custom re-definitions in case of fncychap option Bjarne.

    バージョン 1.5 で変更: formerly, use of fncychap with other styles than Bjarne was dysfunctional.


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.

バージョン 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).

バージョン 1.6 で追加: currently all template variables are unstable and undocumented.