Skip to content

Commit

Permalink
Set default image width/alignment in LaTeX build
Browse files Browse the repository at this point in the history
  • Loading branch information
abidingabi committed Oct 28, 2023
1 parent a0c2d10 commit f8524d3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
43 changes: 43 additions & 0 deletions source/_extensions/default_latex_image_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import Any, Dict

from docutils import nodes

from sphinx.transforms.post_transforms import SphinxPostTransform
from sphinx.application import Sphinx


class DefaultLaTeXImageSettingsTransform(SphinxPostTransform):
"""
Set a default image width for images which do not have a width attribute
manually set. Also center images which are not centered. This only applies
to LaTeX builds.
"""

default_priority = 100 # chosen arbitrarily

def run(self, **kwargs: Any) -> None:
if not self.app.tags.has("latex"):
return

width = self.app.config["default_image_width_latex"]

for node in self.document.findall(nodes.image):
if "width" not in node.attributes:
node.attributes["width"] = width

if (
self.app.config["default_image_centered"]
and "align" not in node.attributes
):
node.attributes["align"] = "center"


def setup(app: Sphinx) -> Dict[str, Any]:
app.add_config_value("default_latex_image_width", "20em", True)
app.add_config_value("default_latex_image_centered", True, True)
app.add_post_transform(DefaultLaTeXImageSettingsTransform)

return {
"parallel_read_safe": True,
"parallel_write_safe": True,
}
1 change: 1 addition & 0 deletions source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"_extensions.localization",
"_extensions.controls_js_sim",
"_extensions.wpilib_release",
"_extensions.default_latex_image_settings",
]

extensions += local_extensions
Expand Down

0 comments on commit f8524d3

Please sign in to comment.