-
-
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
base: master
Are you sure you want to change the base?
Conversation
ec963ba
to
b134c19
Compare
No. vw and vh describe the size of the viewport or page area: In single column text, textwidth == linewidth so you can use "%" to get a percentage of the textwidth.
The "g" formatter is only used with pergentage values that relate to paper size or line width. The default precision with the "g" formatter is 6. So any value 1/100000 < x < 1000000 is safe. Length values outside this range are problematic anyway.
|
I have done the PDF inspection at my locale. It is found at a location which one can reach from the directory indicated by pytest when doing the test. By the way I have now added a separate test and configured testing (i.e. PDF build) to only apply if with Docutils 0.22 (dev). However I am not sure our CI has a configuration for PDF builds plus Docutils HEAD, so this is theoretical so far. But when Docutils 0.22 is released our CI/LaTeX will do the test. So far I test (with success as reported above) at my locale. I wanted to make linting happy but now ruff requires me to put all args on one line, sigh, so a force-push is pending |
For legacy reason |
This sounds reasonable, but we have been using fixed point with either 5 or 3 decimal places and I hesitate modifying legacy code although actually the impact would be minimal and Sphinx users are not in same situation as say AMS editorial offices who want to keep PDF same as 20 years ago. |
@AA-Turner Second commit in this PR allows in principle to test that PDF build does not crash when using the new units in image dimensions, but for the test to not be skipped it would be needed for our CI/LaTeX to use the Docutils HEAD... It may not be that important to bother about this too much. I have also refactored a bit the recent (needed) addition of a second pdflatex run when testing the build of documents. |
% | ||
\def\sphinxchdimen{\dimexpr\fontcharwd\font`0\relax} | ||
% TODO: decide if we want rather \textwidth/\textheight. | ||
\newdimen\sphinxvwdimen |
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 2003 pdflatex
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.
% TODO: decide if we want rather
\textwidth / \textheight
.
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.
@AA-Turner I can not enough recommend eating good quality French apple pie, this has had a tremendous effect on my cognitive abilities and I have pushed a commit so that LaTeX/CI uses Docutils HEAD. This is purely for confirmation that this PR is sane, as building PDF is essential part of checking all is fine, simply checking output mark-up would not be quite convincing enough. So, all is fine https://github.com/sphinx-doc/sphinx/actions/runs/15617838136/job/43995053458?pr=13657#step:9:642
Before we merge this we shoudl arguably revert that latest commit (or revert it after merge). Testing LaTeX only on Docutils HEAD may not be a good idea at time of our releases. |
The x% -> |
Thanks @gmilde for your comment. See though #13661 and #13662. I said above we would not change prior to Sphinx 9, so I will now modify the milestone to Sphinx 9.x but I consider the output so bad (especially #13661) I initially had set it at 8.3.0. Maybe #13661 does require 8.3.0 though... and then #13662 will be solved at same time. I do not think it make sense in LaTeX that image sizes would mysteriously shrink. while text font size is not modified. The HTML/CSS paradigm should not be followed too closely. |
On 13.06.2025 15:06:18, Jean-François B. wrote:
+% TODO: decide if we want rather \textwidth/\textheight.
+\newdimen\sphinxvwdimen
In HTML/CSS the viewport can be fully occupied, not really in LaTeX
...
My simple test with Docutils results in just the opposite:
```rst
.. image:: /usr/share/xine/skins/xine_splash.png
:width: 100 vw
:height: 50Q
:align: center
.. image:: /usr/share/xine/skins/xine_splash.png
:width: 100 %
:height: 50Q
:align: center
```
In LaTeX, I get
```latex
\noindent\makebox[\linewidth][c]{\includegraphics[height=12.5mm,width=1\paperwidth]{/usr/share/xine/skins/xine_splash.png}}
\noindent\makebox[\linewidth][c]{\includegraphics[height=12.5mm,width=1\linewidth]{/usr/share/xine/skins/xine_splash.png}}
```
In the PDF, the image is centered and fits exactly on the page.
In HTML, the image left aligns with the text block and overflows to the right.
But apart from this, typical use will be a certain fraction of the viewport and
there IMO the correct correspondence to 100% browser frame size is 100% page size,
not 100% text area.
* * * * * * *
The correct translation of % to LaTeX is a related but different problem.
In HTML, % is percentage of the containing block.
Regarding bugs #13661 and #13662 -- With Docutils, I get comparable results
for the samples in HTML and PDF. I would call this the expected result and not a bug.
|
I do not think it make sense in LaTeX that image sizes would mysteriously
shrink. while text font size is not modified. The HTML/CSS paradigm should
not be followed too closely.
IMO we should follow CSS as closely as possible for three reasons:
a) it makes it easier to write documents that look
"comparable" in HTML and PDF
b) it is easier to document -- the reStructuredText specification¹
explicitly refers to the CSS units and their definition.²
¹ https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#length-units
² the reference to CSS is "since ages", only the inclusion
of CSS3 units is new in 0.22.
c) it is a sensible set of units for different purposes:
* fixed units (mm, pt, ...) for objects that do not "mysteriously" shrink,
* font-size based units (em, ex) for objects that grow/shrink with the font size,
* % for objects that take a certain proportion of their containing object,
* vh, vw, vmin, vmax for objects that take a certain proportion of the viewport or page.
|
This test does not convince me because it applies uniquely to images. Further in document classes where the leftside margin width is not same as rightside margin width the rescaled image will overflow the page limits on one side. You can not apply this interface to more realistic structures because the What I wanted to say is that it is theoretically possible in HTML/CSS to place things everywhere on the viewport. For LaTeX/PDF output the sole interface from a Sphinx user point of view to go outside the text area is via footnotes. Other things such as header and footers are via the document class, which is a one-shot feature.
I did not understand if "comparable result" means "same as with Sphinx" or "HTML and PDF look about the same".
About "container object", contrarily to what LaTeX people have said for now more than 30 years, the LaTeX mark-up corresponds in no way to a structured document (for example when your start a subsection you have absolutely no built-in way to access the "parent" section then "parent" chapter etc...; same for items in a list, same for everything you can think of basically). Else the LaTeX team would not have such a hard time into making tagging an "out-of-the-box" feature. So we have no notion of "container block" in LaTeX. The only one which comes to mind is the text area. LaTeX is not modeled on nor contains (so far) any equivalent of a Docutils document tree. Then there is also the fact that PDFs are either printed on paper or viewed on screen. I usually set my PDF viewer to auto-enlarge the view to focus on the contents. And what do PDF viewers propose then? To enlarge the view to the width of the text area, not the paper width. Regarding the About Footnotes
|
# 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 comment
The 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 \baselineskip
which has nothing to do with \linewidth
. But it is true that \linewidth
is a somewhat obscure beast in tabulary tables.
In both, HTML and LaTeX, there are many places where Could we agree on the statement: the vw and vh units are of limited use in a reStructuredText document? The "vw" and "vh" units are supported nevertheless, because
Still, I don't think re-purposing these units in the LaTeX writer is right. In contrast to percentage values, the rST specification is very clear about their meaning.
Not out of the box. |
The latter. In Docutils HTML and PDF look about the same. (The main difference is, that Docutils LaTeX tables default to I moved the detailled discussion to the respective issues. |
The CSS definition changed from the idea to make this about perceived size (and hence dependent on the viewing distance) to a fixed value. In CSS3, 1 px = 3/4 pt = 1/96 in. Unfortunately, this differs from the legacy definition in LaTeX. However, in LaTeX, the size of the pixel unit can be configured. |
This matches the high wisdom of the Sphinx maintainers: sphinx/sphinx/builders/latex/constants.py Line 77 in e1bd9cb
It can be configured in Sphinx too, via the Only for being completely precise, let me state that it is not a TeX/LaTeX provided dimension but is provided by the pdfTeX and LuaTeX engines. It is inherited by LaTeX when compiled with these engines. But for example it still does not exist in XeLaTeX:
It exists with
and
You can see it does not have the exact same value as in LuaLaTeX! One can do the following amusing experiment:
In fact the PDFTeX manual now says "The default value of Apart from xelatex also uplatex does not have a native understanding of the At Sphinx we have been doing this in the latex template to account for all possibilities and to set the default to match CSS3: sphinx/sphinx/templates/latex/latex.tex.jinja Lines 13 to 15 in e1bd9cb
|
Relates: #13656
cc @gmilde. Two questions
\paperwidth
and\textwidth
forvw
?g
formatter, there is a slight chance this will use scientific notation on output, although very improbable but if it happens the PDF build will break. Have you estimated it is safe enough? We could follow suit.obsolete part removed as now the test has been added in a second commit, although I don't think it will be run by our CI/LaTeX, but it will be run when Docutils 0.22 is released
a third provisory commit triggers usage of Docutils HEAD for CI/LaTeX to confirm all is fine