简体   繁体   中英

Font scaling, variables, calculations in LaTeX

I'm currently writing my CV and I'm running out of space. I wish to change the font in the whole CV template, but I want to do it proportionally. For example, some headings are 12pt, subheadings are 11pt. I want to be able to change them to 0.9 * 12 pt, 0.9 * 11 pt, aka to scale them accordingly. I want to change

\fontsize{12pt}{1em}

to

\fontsize{(0.9 * 12)pt}{1em}

, where \fontsize is a command from anyfontsize package.

But I end up with weird results and lots of logs/mistakes. Is there a way to ensure that '*' will produce a number, like in "regular" programming languages?

Other things I've tried include:

\newcommand{\myfont}[2]{\fontsize{0.9*#1pt}{0.9*#2}em}
\myfont{12}{1}

But it doesn't work the way I expect it to. I also tried adding brackets () and making a new command \newcommand{\mymultiply}[2]{#1*#2}

Is there an elegant and neat way to do this? Is there a "regular" way in LaTeX of doing actual calculations, or should I avoid this approach? Thanks in advance

3 possible approaches:

  • I would avoid using hard coded font sizes altogether. If you instead us font commands like \smaller or \larger you can simply change the overall size with documentclass options like 10pt or 12pt and all the fonts in the document will scale, including sections etc.

  • if you store your base fontsize in a length, you could use \dimexpr to make the calculations

  • if for whatever reason you don't want to create a new length, you use an expl3 command to do the calculations


\documentclass[
%10pt
12pt
]{article}  

\usepackage{anyfontsize}

\ExplSyntaxOn
  \cs_new_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff

\begin{document}  

\section{section}

section

\newlength{\foo}
\setlength{\foo}{12pt}
\fontsize{\dimexpr.9\foo\relax}{\dimexpr1.08\foo \relax}\selectfont

test

\fontsize{\dimexpr\fpeval{.9*12}pt\relax}{\dimexpr\fpeval{.9*1.2*12}pt\relax}\selectfont

test

\end{document}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM