# How to create a newcommand for fontsize?

I am trying to create a command to modify fontsize with one parameter, it seems that the paramater is not expanding properly:

\newcommand{\mycfs}[1]{\fontsize{#1pt}{1.2*#1pt}}

-
Note that the normal size macros store themselves as \@currsize. Most likely so they can be restored. This is not the case in your code or the one is the answers. However, I can't tell you if and what impact this has. – Martin Scharrer Jul 3 '11 at 8:52

Omit the * if you use a factor. Further, introduce a length macro that can be multiplied. If needed, add \selectfont to make it effective:

\newlength{\mysize}
\newcommand{\mycfs}[1]{\setlength{\mysize}{#1pt}%
\fontsize{\mysize}{1.2\mysize}\selectfont}

-
Does that work with literal sizes? For example, if I put \mycfs{10} won't I get a 1.21 pt baseline separation? – Joseph Wright Aug 23 '10 at 18:46
that's what I think as well. – Taco Hoekwater Aug 23 '10 at 18:49
Good point, Joseph! I've edited it using a length macro. – Stefan Kottwitz Aug 23 '10 at 18:59
There's even a neat trick that the LaTeX kernel does that allows you to have default units that can be overridden. \@defaultunits\mysize#1pt\relax\@nnil I suspect that \@defaultunits\setlength\mysize{#1pt}\@nnil would work, but probably not with the calc package. The upshot of this is you can use \mycfs{.5in} (or whatever), if you really want. – TH. Aug 23 '10 at 23:24
@user942, depending on the effect you want, you might try \fontsize{\mysize}{\f@baselineskip} in place of \fontsize{\mysize}{1.2\mysize} (wrapped between \makeatother .. \makeatletter, of course). If using LaTeX's internal \f@baselineskip gives you what your want (it mightn't do), then you can dispense with the \mysize length entirely as well as all the paraphernalia code. – Geoffrey Jones Aug 24 '10 at 15:31

This works, using e-TeX functionality to calculate the baseline skip on the fly:

\newcommand{\mycfs}[1]{\fontsize{#1pt}{\dimexpr 1.2pt*#1pt\relax}}


Or this solution, without e-TeX, that uses an intermediate dimension register so is a bit more complex:

\newdimen\mycfsdim
\newcommand{\mycfs}[1]{\mycfsdim=#1pt \mycfsdim=1.2\mycfsdim
\fontsize{#1pt}{\the\mycfsdim}\selectfont}

-
I have to say my reaction would be 'use e-TeX', but of course if your sending to a publisher they may not have the extensions! – Joseph Wright Aug 23 '10 at 18:49

First of all: In general it is not recommend to freely adjust the font size. Also you of course need a scalable font, otherwise the next fitting one will be chosen.

The used 1.2*#1pt doesn't work in general. You can use 1.2\somedimension to get the multiplication.

\newlength{\mylength}
\makeatletter
\newcommand{\mycfs}[1]{%
\setlength{\mylength}{#1pt}%
\setlength{\mylength}{1.2\mylength}%
\fontsize{#1}{\mylength}%
\selectfont
}
\makeatother


However, some but not all font size macros (\normalsize, \small and \footnotesize) also change other settings like \abovedisplayskip, \belowdisplayskip, \abovedisplayshortskip and \belowdisplayshortskip. To get consistent behavior to would be better to switch to a know font size before you change to yours, e.g. add \normalsize at the front of your macro code. You could also adjust the above parameters by yourself, if you know how to do it; correctly I mean.

All font size macros also store themselves into \@currsize , e.g. \small includes \let\@currsize\small, but only if \ifx \protect \@typeset@protect. This is done by calling \@setfontsize which isn't usable in your case. However, this could be added manually:

\newlength{\mylength}
\makeatletter
\newcommand{\mycfs}[1]{%
\normalsize
\ifx\protect\@typeset@protect
\def\@currsize{\mycfs{#1}}%
\fi
\setlength{\mylength}{#1pt}%
\setlength{\mylength}{1.2\mylength}%
\fontsize{#1}{\mylength}%
\selectfont
}
\makeatother


You can further improve this by allowing the input to be a number in pt or any lengths using LaTeX's \@defaultunits (as it is done by \fontsize itself):

\newlength{\mylength}
\makeatletter
\newcommand{\mycfs}[1]{%
\normalsize
\@defaultunits\mylength=#1pt\relax\@nnil
\edef\@tempa{{\strip@pt\mylength}}%
\ifx\protect\@typeset@protect
\edef\@currsize{\noexpand\mycfs\@tempa}% store calculated size
\fi
\mylength=1.2\mylength
\edef\@tempa{\@tempa{\strip@pt\mylength}}%
\@tempa
\expandafter\fontsize\@tempa
\selectfont
}
\makeatother

-