josephwright / siunitx

A comprehensive (SI) units package for LaTeX
LaTeX Project Public License v1.3c
351 stars 26 forks source link

Detect all font weights #306

Closed tweh closed 3 years ago

tweh commented 7 years ago

At the moment the test to detect the font weight ist done by checking for the first letter of \f@series, which works if the document uses only \mdseries and \bfseries. For an additional use of a list shape it works as long as it is named \lseries (I prefer \ltseries, since other weights have two letters as well). However if the document uses more than these weights the test must fail unfortunately.

siunitx-font-detection pdf seite 1 von 1 2017-06-05 11-52-03

Here’s an example using Open Sans (free) and fontspec to access all weights. It also includes an alternative definition of \__siunitx_detect_font_weight_text: that works in my test document but maybe has some disadvantages I can’t see at the moment.

% !TeX program = xelatex
\documentclass[parskip=full]{scrartcl}

\usepackage{fontspec}
\setmainfont{Open Sans}[
   LetterSpace = 2,
   FontFace = {lt}{n}{* Light},
   FontFace = {sb}{n}{* Semibold},
   FontFace = {bf}{n}{* Bold},
   FontFace = {xb}{n}{* Extrabold},
   FontFace = {lt}{it}{* Light Italic},
   FontFace = {sb}{it}{* Semibold Italic},
   FontFace = {bf}{it}{* Bold Italic},
   FontFace = {xb}{it}{* Extrabold Italic},
]
\DeclareRobustCommand\ltseries{\fontseries{lt}\selectfont}
\DeclareTextFontCommand{\textlt}{\ltseries}
\DeclareRobustCommand\sbseries{\fontseries{sb}\selectfont}
\DeclareTextFontCommand{\textsb}{\sbseries}
\DeclareRobustCommand\xbseries{\fontseries{xb}\selectfont}
\DeclareTextFontCommand{\textxb}{\xbseries}

\usepackage{siunitx}
\sisetup{detect-all}

\let\lseries\ltseries

\ExplSyntaxOn\makeatletter

   % POSSIBLE SOLUTION
%   \cs_generate_variant:Nn \tl_if_in:nnTF { noTF }
%   \cs_set_protected:Npn \__siunitx_detect_font_weight_text: {
%     \tl_if_in:noTF { sb bf xb } { \f@series }
%       {
%         \cs_set:Nn \__siunitx_font_weight: { \boldmath \use:c { \f@series series } }
%       }
%       {
%         \cs_set:Nn \__siunitx_font_weight: { \use:c { \f@series series } }
%       }
%   }

   \NewDocumentCommand { \printseries } { } {
      series=\f@series
   }

\makeatother\ExplSyntaxOff

\begin{document}
Upright shapes:

\textlt{Light: \SI{1.234e-5}{\micro\farad} [\printseries]} (ok with \verb|\let\lseries\ltseries|)

Normal: \SI{1.234e-5}{\micro\farad} [\printseries] (ok)

\textsb{Semibold: \SI{1.234e-5}{\micro\farad} [\printseries]} (wrong weight)

\textbf{Bold: \SI{1.234e-5}{\micro\farad} [\printseries]} (ok)

\textxb{Extrabold: \SI{1.234e-5}{\micro\farad} [\printseries]} (wrong weight)

\bigskip
Same for italic shapes:

\itshape
\textlt{Light Italic: \SI{1.234e-5}{\micro\farad}} (ok with \verb|\let\lseries\ltseries|)

Normal Italic: \SI{1.234e-5}{\micro\farad} (ok)

\textsb{Semibold Italic: \SI{1.234e-5}{\micro\farad}} (wrong weight)

\textbf{Bold Italic: \SI{1.234e-5}{\micro\farad}} (ok)

\textxb{Extrabold Italic: \SI{1.234e-5}{\micro\farad}} (wrong weight)
\end{document}

See also siunitx does not detect semi-bold font on TeX.SX.

josephwright commented 7 years ago

One of the things here that is tricky is that the 'core' LaTeX set up only offers two weights, and it's not easy to know how to fully handle other cases. For example, what happens for math mode if we detect xb? There is not \xboldmath or similar ... As you might guess, the original approach (before \lseries got added) was entirely based around what is available in CM/'out of the box' LaTeX.

Of course, one then gets into the entire business that math mode is tricky when you start worrying about weight and shape: depending on you background, this may be something you don't vary as part of design but as part of semantics. (I'm not really from that background.)

Probably you are correct in that in cases where people use xb or similar it's reasonable to do ones best to pick them up. Any idea if any of the above set \maht@version or indeed whether to extend support in that area?

(BTW, for v3 I'm likely to use an approach which will work rather differently at the low level here, but where the outcome should be much as you suggest.)

josephwright commented 7 years ago

Actually, I wonder if I might be best using the new approach here anyway: I'll do some tests and see if I can get it to come off!

tweh commented 7 years ago

Hi,

I absolut understand your concerns about math. How about setting things up to work out of the box with the two weights, but having the possibility to detect other weights to. E.g.

In that case the user could decide wether a math version is changed or not (depending on if bold math has another meaning in his document or not).

u-fischer commented 7 years ago

Imho one should also consider the case that users want the font(series) inside math to follow the mathversion even if they use the text font to print the unit. That means that

      \sisetup{mode=text,detect-weight-or-mathversion,...}
       $ \SI ... $

should use a font \mathnormaltextfont or \mathboldtextfont depending on the math version and not on the outside \f@series.

josephwright commented 6 years ago

The v3 code should work here in text mode: as it works by only altering what is needed, turning off control of weight will preserve weight 'as given' in text mode. Matching to math mode is more tricky: some discussion with@u-fischer suggests that should be handled separately anyway!

tweh commented 6 years ago

Nice to hear. Thanks!

josephwright commented 6 years ago

This is working in v3 with LuaTeX, largely because the approach to font control is entirely re-written. For some reason it's not currently happy in XeTeX, so I'll leave open for the present and try to track down (I suspect 'not my fault' though).

As noted in other issues, I'm planning a release somewhere around the turn of the year, thought there will likely be an alpha soon-ish. Feedback on the font control options/outcomes will be welcome when I do that!

josephwright commented 6 years ago

Now sorted in XeTeX too: I'm closing on the grounds v3 will sort this.

FrankMittelbach commented 4 years ago

Just a general comment not related to siunitx but to the FontFace mappings:

I'm currently hard working on getting back to standard names across NFSS because only if everybody uses the same set as series values exchanging one font with another works well and the tools like siunitx can then map things. All fonts produced through autoinst (which is the majority are now back to that and I hope that the few remaining j k t etc

"lt" is not the name for light it is "l"!

Full official set is ul el l sl m sb b eb ub (ultra-light to ultra-bold)

josephwright commented 4 years ago

@FrankMittelbach Thanks: I'll adjust the siunitx code to recognise this set

FrankMittelbach commented 4 years ago

as I said elsewhere it is unfortunately not that easy because of the (bad) decision I made long ago:

if the weight is "m" (medium) then this "m" is dropped if the width is not "medium" too, so

medium weight condensed is not mc (as it should have been) but "c" and so on

so if the weight is "m" then you see only the width in \f@series in other words

uc ec c sc m sx x ex ux

sorry but 30 years ago every byte was precious to fit into the available memory.

josephwright commented 4 years ago

I can make this work!

FrankMittelbach commented 4 years ago

sure, no doubt. just saying what the expected logic is