12

I need a way to get the current fontsize as a length.

I hope I didn't overlook anything that already provides this. What I found that I can do

\makeatletter
\newlength{\fsize}
\setlength{\fsize}{\f@size pt}
\makeatother

but it will be set to a fixed length that does not update when font size changes.

Now I tried to put it into a command:

\makeatletter
\newlength{\fsizeb}
\setlength{\fsizeb}{\f@size pt}
\newcommand\fsize{\fizeb}
\makeatother

(still works as a length but also does not update - not sure if this is actually valid, maybe this is my first fault)

And finally I tried

\makeatletter
\newlength{\fsizeb}
\newcommand\fsize{%
    \setlength{\fsizeb}{\f@size pt}%
    \fsizeb%
}

which fails and does not seem to return a length.

I assume I'm doing something horribly wrong (I'm quite new to this part of LaTeX, so I hope you can tell me wether what I want is at all possible and give me some pointers how to implement...)


As for why I (think to) need this: I want to be able to set \baselineskip to a specific multiple of the font size (similarly as the line-height property works in CSS/SVG) like so (with the solution suggested by Manuel in the comments):

\documentclass{scrartcl}

\makeatletter
\newcommand*\fsize{\dimexpr\f@size pt\relax}
\makeatother

\begin{document}

\setlength{\baselineskip}{1\fsize}
\verb=\setlength{\baselineskip}{1\fsize}= -- line-height: 1; bars touching exactly\\\textbar\\\textbar

\setlength{\baselineskip}{1.25\fsize}
\verb=\setlength{\baselineskip}{1.25\fsize}= -- line-height: 1.25; (relative to font-size)\\\textbar\\\textbar

\end{document}

If there are yet easier solutions I'd be happy to hear.

4
  • 3
    why can you not use \f@size as it is, why do you need to use a dimen register? (if you use a register you are going to have to patch \selectfont to update the register every time anything changes but it is hard to think why you would need this) Jan 11, 2018 at 18:55
  • 1
    I would use just \makeatletter\newcommand\fsize{\f@size pt}\makeatother Jan 11, 2018 at 19:54
  • 3
    @DavidCarlisle Or if he actually needs a “length” may be \newcommand*\fsize{\dimexpr\f@size pt\relax}? Although I don't know what could be the case.
    – Manuel
    Jan 11, 2018 at 19:55
  • @Manuel This was exactly what I was looking for, thank you (feel free to add it as an answer)! I added an explanation on what I want to do with it and why I want to have the result as a length. Jan 12, 2018 at 1:38

3 Answers 3

13
\makeatletter
\newcommand*\fsize{\dimexpr\f@size pt\relax}
\makeatother

This way you can use the macro like \setlength{\baselineskip}{1.33\fsize}.

1
  • My favourite answer.
    – AlexG
    Jan 12, 2018 at 9:02
9

\selectfont is the switch the changes the font. You can tie into this and update \fsize:

enter image description here

\documentclass{article}

\usepackage{letltxmacro}

\newlength{\fsize}
% https://tex.stackexchange.com/q/88001/5764
\LetLtxMacro\oldselectfont\selectfont
\makeatletter
\DeclareRobustCommand{\selectfont}{\oldselectfont\setlength{\fsize}{\f@size pt}}
\makeatother

\begin{document}

\tiny\the\fsize

\scriptsize\the\fsize

\footnotesize\the\fsize

\small\the\fsize

\normalsize\the\fsize

\large\the\fsize

\Large\the\fsize

\LARGE\the\fsize

\Huge\the\fsize

\end{document}
1
  • 1
    Confirmed working. The solution posted by Manuel in the comments is a lot more concise, though. Jan 12, 2018 at 1:39
8

If you set the length, it won't change until you reset it. What you need is a macro:

\makeatletter
\newcommand{\currentfsize}{\f@size pt}
\makeatother

Example (using the code by Werner):

\documentclass{article}

\makeatletter
\newcommand{\currentfsize}{\f@size pt}
\makeatother

\newdimen\fsize
\newcommand{\setfsize}{\setlength{\fsize}{\currentfsize}}

\begin{document}

\setfsize % should be 10pt

\tiny\currentfsize

\scriptsize\currentfsize

\footnotesize\currentfsize

\small\currentfsize

\normalsize\currentfsize

\large\currentfsize

\Large\currentfsize

\LARGE\currentfsize

\Huge\currentfsize

\the\fsize % should be 10pt (printed in \Huge size)

\end{document}

As you see, setting \fsize at the beginning doesn't change its value.

enter image description here

2
  • This does not give a length - as I want to be able to write e.g. 1.25\fsize that does not completely solve my problem. Jan 12, 2018 at 1:41
  • 2
    1.25\dimexpr\currentfsize\relax
    – egreg
    Jan 12, 2018 at 9:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .