Skip to content

Instantly share code, notes, and snippets.

@infinity0
Created November 8, 2015 22:03
Show Gist options
  • Save infinity0/f987f440ec786dc72c2a to your computer and use it in GitHub Desktop.
Save infinity0/f987f440ec786dc72c2a to your computer and use it in GitHub Desktop.
Sphinx LaTeX workarounds
# -- Options for LaTeX output ---------------------------------------------
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
'papersize': 'a4paper',
# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
'preamble': """
\\input{../../source/_include/unicode.tex}
\\usepackage{enumitem}
\\setlist[description]{style=nextline}
""",
# Latex figure (float) alignment
#'figure_align': 'htbp',
# Get rid of so many blank pages.
'classoptions': ',openany,oneside',
'babel': '\\usepackage[english]{babel}',
}
################ begin workaround ################
# hacky latex workarounds
latex_post_sed_expr = [
# "style=nextline" bug for definition lists
# https://tex.stackexchange.com/questions/198165/newline-after-each-element-in-description-environment
r"s,\\leavevmode\\begin{\([^}]*\)},\\leavevmode\\vspace{-\\baselineskip}\\begin{\1},g",
# certain uses of DUlineblock have too much vertical padding
r"/^\\end{DUlineblock}$/{N;s/\n$/\\vspace{-\\baselineskip}\n/;b}",
r"$!N;s/^\n\(\\begin{DUlineblock}{0em}\)/\n\\vspace{-\\baselineskip}\1/;P;D",
]
import sphinx.builders.latex
import subprocess
_LaTeXBuilder = sphinx.builders.latex.LaTeXBuilder
class LaTeXBuilder(_LaTeXBuilder):
def write(self, *args, **kwargs):
_LaTeXBuilder.write(self, *args, **kwargs)
for _, targetname, _, _, _ in self.document_data:
dst = os.path.join(self.outdir, targetname)
subprocess.check_call(["sed"] + ["-e"+e for e in latex_post_sed_expr] + ["-i", dst])
sphinx.builders.latex.LaTeXBuilder = LaTeXBuilder
################# end workaround #################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment