Skip to content

Commit

Permalink
New configuration value for additional latex files
Browse files Browse the repository at this point in the history
New latex preamble fallback behavior
  • Loading branch information
philexander committed Apr 15, 2024
1 parent d4086e7 commit 1a91890
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,22 @@ Additionally, the following configuration values are supported:

tikz_latex_preamble = ‹string›

.. note:: If ``tikz_latex_preamble`` is not configured, then the LaTeX preamble
automatically falls back to ``latex_elements['preamble']``.

.. note:: LaTeX preamble code is best written as a raw string ``r'‹LaTeX code›'``
or a raw multi-line string ``r'''‹multiple lines of LaTeX code›'''``. This
tells Python to disable transformation of backslash escape sequences such as
``\n`` into special characters such as newline.

* Copy files to the directory that is used for building the Ti\ *k*\ Z picture,
analogous to ``latex_additional_files``::

`tikz_additional_files = ‹list of strings›

.. note:: If ``tikz_additional_files`` is not configured, then the list of files to
be copied automatically falls back to ``latex_additional_files``.

* To support ``\includegraphics{‹file›}`` within a Ti\ *k*\ Z picture, you have to
configure the directory path(s) where the ``‹file›``\ s reside by setting::

Expand Down
31 changes: 26 additions & 5 deletions sphinxcontrib/tikz.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
except ImportError:
from sphinx.util import ensuredir

from sphinx.util.fileutil import copy_asset_file

from glob import glob

_Win_ = sys.platform[0:3] == 'win'
Expand Down Expand Up @@ -272,7 +274,10 @@ def render_tikz(self, node, libs='', stringsubst=False):

latex = DOC_HEAD % libs
latex += self.builder._tikz_preamble
latex += config.tikz_latex_preamble
if config.tikz_latex_preamble:
latex += config.tikz_latex_preamble
else:
latex += config.latex_elements['preamble']
latex += DOC_BODY % tikz
latex = latex.encode('utf-8')

Expand Down Expand Up @@ -439,7 +444,8 @@ def cleanup_tempdir(app, exc):
def builder_inited(app):
app.builder._tikz_tempdir = tempfile.mkdtemp()
app.builder._tikz_preamble = ''
extgfxpath = app.builder.config.tikz_includegraphics_path
config = app.builder.config
extgfxpath = config.tikz_includegraphics_path
if not extgfxpath == '':
if isinstance(extgfxpath, str):
extgfxpath = [extgfxpath]
Expand All @@ -458,18 +464,32 @@ def builder_inited(app):
sty.write(r"\RequirePackage{amsmath}" + "\n")
sty.write(r"\RequirePackage{amsfonts}" + "\n")
sty.write(r"\RequirePackage{pgfplots}" + "\n")
tikzlibs = app.builder.config.tikz_tikzlibraries
tikzlibs = config.tikz_tikzlibraries
tikzlibs = tikzlibs.replace(' ', '')
tikzlibs = tikzlibs.replace('\t', '')
tikzlibs = tikzlibs.strip(', ')
sty.write("\\usetikzlibrary{%s}\n" % tikzlibs)
sty.write(app.builder._tikz_preamble)
sty.write(app.builder.config.tikz_latex_preamble + "\n")
latex_preamble = config.tikz_latex_preamble \
if config.tikz_latex_preamble \
else config.latex_elements['preamble']
sty.write(latex_preamble + "\n")
sty.close()

app.builder.config.latex_additional_files.append(sty_path)
config.latex_additional_files.append(sty_path)
config.latex_additional_files.extend(
config.tikz_additional_files)
app.add_latex_package("sphinxcontribtikz")

if app.builder.name == "html":
additional_files = config.tikz_additional_files \
if config.tikz_additional_files \
else config.latex_additional_files
if additional_files:
for filename in additional_files:
copy_asset_file(path.join(app.builder.confdir, filename),
app.builder._tikz_tempdir)


def which(program):
if sys.platform == "win32" and not program.endswith(".exe"):
Expand Down Expand Up @@ -502,6 +522,7 @@ def setup(app):
app.add_role('tikz', tikz_role)
app.add_directive('tikz', TikzDirective)
app.add_config_value('tikz_latex_preamble', '', 'env')
app.add_config_value('tikz_additional_files', [], 'env')
app.add_config_value('tikz_tikzlibraries', '', 'env')
app.add_config_value('tikz_transparent', True, 'html')
app.add_config_value('tikz_includegraphics_path', '', 'env')
Expand Down

0 comments on commit 1a91890

Please sign in to comment.