Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default image width/alignment in LaTeX build #2370

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_latex_image_width"]

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

if (
self.app.config["default_latex_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", "25em", 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