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
Changes from 1 commit
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
Next Next commit
Set default image width/alignment in LaTeX build
abidingabi committed Oct 28, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit f8524d399809cf7adeb0ef83c4379a4eb442ed27
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
@@ -65,6 +65,7 @@
"_extensions.localization",
"_extensions.controls_js_sim",
"_extensions.wpilib_release",
"_extensions.default_latex_image_settings",
]

extensions += local_extensions