-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
LaTeX: support CSS3 length units #13657
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
Open
jfbu
wants to merge
3
commits into
sphinx-doc:master
Choose a base branch
from
jfbu:latex_css3_units
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -305,6 +305,11 @@ def escape_abbr(text: str) -> str: | |
|
||
def rstdim_to_latexdim(width_str: str, scale: int = 100) -> str: | ||
"""Convert `width_str` with rst length to LaTeX length.""" | ||
# MEMO: the percent unit is interpreted here as a percentage | ||
# of \linewidth. Let's keep in mind though that \linewidth | ||
# is dynamic in LaTeX (e.g. smaller in lists), and may even | ||
# be zero in some contexts (some table cells). This is a legacy | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will have to be corrected. I apologize but it seems I made a confusion with some other LaTeX length, the |
||
# situation perhaps best not changed (2025/06/11). | ||
match = re.match(r'^(\d*\.?\d*)\s*(\S*)$', width_str) | ||
if not match: | ||
raise ValueError | ||
|
@@ -318,6 +323,8 @@ def rstdim_to_latexdim(width_str: str, scale: int = 100) -> str: | |
res = '%sbp' % amount # convert to 'bp' | ||
elif unit == '%': | ||
res = r'%.3f\linewidth' % (float(amount) / 100.0) | ||
elif unit in {'ch', 'rem', 'vw', 'vh', 'vmin', 'vmax', 'Q'}: | ||
res = rf'{amount}\sphinx{unit}dimen' | ||
else: | ||
amount_float = float(amount) * scale / 100.0 | ||
if unit in {'', 'px'}: | ||
|
@@ -326,8 +333,16 @@ def rstdim_to_latexdim(width_str: str, scale: int = 100) -> str: | |
res = '%.5fbp' % amount_float | ||
elif unit == '%': | ||
res = r'%.5f\linewidth' % (amount_float / 100.0) | ||
elif unit in {'ch', 'rem', 'vw', 'vh', 'vmin', 'vmax', 'Q'}: | ||
res = rf'{amount_float:.5f}\sphinx{unit}dimen' | ||
else: | ||
res = f'{amount_float:.5f}{unit}' | ||
# MEMO: non-recognized units will in all probability end up causing | ||
# a low-level TeX error. The units not among those above which will | ||
# be accepted by TeX are sp (all TeX dimensions are integer multiple | ||
# of 1sp), em and ex (font dependent), bp, cm, mm, in, and pc. | ||
# Non-CSS units are cc, nc, dd, and nd. Also the math only mu, which | ||
# is not usable for example for LaTeX length assignments. | ||
return res | ||
|
||
|
||
|
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,25 @@ | ||
============= | ||
TEST IMAGES | ||
============= | ||
|
||
test-latex-images-css3-lengths | ||
============================== | ||
|
||
.. image:: img.png | ||
:width: 10.03ch | ||
:height: 9.97rem | ||
|
||
.. image:: img.png | ||
:width: 60vw | ||
:height: 10vh | ||
|
||
.. image:: img.png | ||
:width: 10.5vmin | ||
:height: 10.5vmax | ||
|
||
.. image:: img.png | ||
:width: 195.345Q | ||
|
||
.. image:: img.png | ||
:width: 195.345Q | ||
:scale: 50% |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LaTeXnical note: we could define (via a suitable
\edef
)\sphinxvwdimen
and all the others as a macro expanding to some\dimexpr...\relax
, but since TeXLive 2003pdflatex
embeds e-TeX extensions (last released in 1999, but LaTeX documentation in books or online is woefully outdated and almost never mention them) and, besides providing\dimexpr
and\numexpr
, these extensions have lifted the restriction on the number of\newdimen
one can use. e-TeX also provided\fontcharwd
used above in the\sphinxchdimen
, it will dynamically adapt to the font used at that point in document; although Sphinx documents will not likely change fonts, but if in a footnote this will take care of the smaller font size.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As in HTML/CSS, vw and vh relate to the viewport (~ screen size) and not the text area, I recommend keeping the conversion to
\paperwidth / \paperheight
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In HTML/CSS the viewport can be fully occupied, not really in LaTeX apart from margin notes for which we have no interface. This is why
\textwidth
and\textheight
are a serious contender in my opinion. I do not have strong opinion though.