-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set default image width/alignment in LaTeX build
- Loading branch information
1 parent
a0c2d10
commit f8524d3
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters