Skip to content

Commit ec963ba

Browse files
committed
LaTeX: support CSS3 length units
1 parent 118f4a1 commit ec963ba

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

sphinx/templates/latex/latex.tex.jinja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
\ifdefined\pdfimageresolution
1717
\pdfimageresolution= \numexpr \dimexpr1in\relax/\sphinxpxdimen\relax
1818
\fi
19+
\newdimen\sphinxremdimen\sphinxremdimen = <%= pointsize%>
1920
%% let collapsible pdf bookmarks panel have high depth per default
2021
\PassOptionsToPackage{bookmarksdepth=5}{hyperref}
2122
<% if use_xindy -%>

sphinx/texinputs/sphinx.sty

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
% by the Sphinx LaTeX writer.
1010

1111
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
12-
\ProvidesPackage{sphinx}[2025/04/24 v8.3.0 Sphinx LaTeX package (sphinx-doc)]
12+
\ProvidesPackage{sphinx}[2025/06/11 v8.3.0 Sphinx LaTeX package (sphinx-doc)]
1313

1414
% provides \ltx@ifundefined
1515
% (many packages load ltxcmds: graphicx does for pdftex and lualatex but
@@ -1218,5 +1218,25 @@
12181218
% FIXME: this line should be dropped, as "9" is default anyhow.
12191219
\ifdefined\pdfcompresslevel\pdfcompresslevel = 9 \fi
12201220

1221-
1221+
%%% SUPPORT FOR CSS3 EXTRA LENGTH UNITS
1222+
% cf rstdim_to_latexdim in latex.py
1223+
%
1224+
\def\sphinxchdimen{\dimexpr\fontcharwd\font`0\relax}
1225+
% TODO: decide if we want rather \textwidth/\textheight.
1226+
\newdimen\sphinxvwdimen
1227+
\sphinxvwdimen=\dimexpr0.01\paperwidth\relax
1228+
\newdimen\sphinxvhdimen
1229+
\sphinxvhdimen=\dimexpr0.01\paperheight\relax
1230+
\newdimen\sphinxvmindimen
1231+
\sphinxvmindimen=\dimexpr
1232+
\ifdim\paperwidth<\paperheight\sphinxvwdimen\else\sphinxvhdimen\fi
1233+
\relax
1234+
\newdimen\sphinxvmaxdimen
1235+
\sphinxvmaxdimen=\dimexpr
1236+
\ifdim\paperwidth<\paperheight\sphinxvhdimen\else\sphinxvwdimen\fi
1237+
\relax
1238+
\newdimen\sphinxQdimen
1239+
\sphinxQdimen=0.25mm
1240+
% MEMO: \sphinxremdimen is defined in the template as it needs
1241+
% the config variable pointsize.
12221242
\endinput

sphinx/writers/latex.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,11 @@ def escape_abbr(text: str) -> str:
305305

306306
def rstdim_to_latexdim(width_str: str, scale: int = 100) -> str:
307307
"""Convert `width_str` with rst length to LaTeX length."""
308+
# MEMO: the percent unit is interpreted here as a percentage
309+
# of \linewidth. Let's keep in mind though that \linewidth
310+
# is dynamic in LaTeX (e.g. smaller in lists), and may even
311+
# be zero in some contexts (some table cells). This is a legacy
312+
# situation perhaps best not changed (2025/06/11).
308313
match = re.match(r'^(\d*\.?\d*)\s*(\S*)$', width_str)
309314
if not match:
310315
raise ValueError
@@ -318,6 +323,8 @@ def rstdim_to_latexdim(width_str: str, scale: int = 100) -> str:
318323
res = '%sbp' % amount # convert to 'bp'
319324
elif unit == '%':
320325
res = r'%.3f\linewidth' % (float(amount) / 100.0)
326+
elif unit in {'ch', 'rem', 'vw', 'vh', 'vmin', 'vmax', 'Q'}:
327+
res = rf'{amount}\sphinx{unit}dimen'
321328
else:
322329
amount_float = float(amount) * scale / 100.0
323330
if unit in {'', 'px'}:
@@ -326,8 +333,16 @@ def rstdim_to_latexdim(width_str: str, scale: int = 100) -> str:
326333
res = '%.5fbp' % amount_float
327334
elif unit == '%':
328335
res = r'%.5f\linewidth' % (amount_float / 100.0)
336+
elif unit in {'ch', 'rem', 'vw', 'vh', 'vmin', 'vmax', 'Q'}:
337+
res = rf'{amount_float / 100.0:.5f}\sphinx{unit}dimen'
329338
else:
330339
res = f'{amount_float:.5f}{unit}'
340+
# MEMO: non-recognized units will in all probability end up causing
341+
# a low-level TeX error. The units not among those above which will
342+
# be accepted by TeX are sp (all TeX dimensions are integer multiple
343+
# of 1sp), em and ex (font dependent), bp, cm, mm, in, and pc.
344+
# Non-CSS units are cc, nc, dd, and nd. Also the math only mu, which
345+
# is not usable for example for LaTeX length assignments.
331346
return res
332347

333348

0 commit comments

Comments
 (0)