Font scaling, variables, calculations in LaTeX - 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}

Related

LaTeX - Set small fontsize for the entire document

I want to change the font size to small for the entire document. I have searched online. But I am not able to find a reasonable (one line) answer. Is this possible to change the font size of latex document by only one command.? Thanks.
I think either
\documentclass[10pt]{article}
where you replace 10 with whatever font point-size you want, or, perhaps less likely,
\begin{document}
\small
where you replace \small with whatever size-command you want, might be what you are looking for.

get height on a block of latex output

I am trying to determine how to get the height on a block of latex output (not the whole document, and not the code..but rather a block of output). As an example of what I am trying to accomplish: i were to have the latex code
$\sum_{i=0}^\infty \frac{1}{n}>\infty$ \newline hello world \newline hello universe
The height of the above block of text is dependent on a number of things-font, margin size, and of course what the text is, as changing any of these parameters changes how many inches that output would be, but with default formatting its output would be something like 2 inches high.
I am hoping there is a package that does this!
Usually, the trick is to put whatever you want to measure into a box and then simply not typeset the box, but measure it:
\newdimen\height
\setbox0=\hbox{\Huge Hello, World!}
\height=\ht0 \advance\height by \dp0
The height is: \the\height
I think this will work:
\newlength{\somenamehere}
\settoheight{\somenamehere}{\hbox{...}}
Where ... is your content you like to measure. And you can then use \somenamehere as the height of that content.
Example:
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\begin{document}
\newlength{\heightofhw}
\settoheight{\heightofhw}{\hbox{Hello World!}}
Value = \the\heightofhw
\end{document}
Will output:
Value = 6.8872pt
Note:
Values of lengths are stored as points, and 1 inch ≈ 72.27 pt
This does not require any additional packages.
Update:
Use \hbox to correctly calculate the height of a different sized environment, but it won't work with newlines :-(

How do I control the width of a \caption{} in a float? (LaTeX)

I am looking for a simple way to control the width of a \caption{} inside a float in LaTeX without using the {caption} package. Any clues?
(EDIT): The problem I am having is that the built-in captions are too narrow.
In general, to control the width of objects in TeX, you can use a minipage, for instance:
\begin{minipage}[t]{1.85in}
\caption{ Why would you eat a pickle? }
\end{minipage}
However, with a caption you might need to do something different as it could very well be a macro.
To change the formatting of the standard LaTeX classes without using a package, you need to look at the source of the standard LaTeX classes and alter the original definitions in your own document. Printing captions is done by the macro \#makecaption, which has definition (in article.cls):
\long\def\#makecaption#1#2{%
\vskip\abovecaptionskip
\sbox\#tempboxa{#1: #2}%
\ifdim \wd\#tempboxa >\hsize
#1: #2\par
\else
\global \#minipagefalse
\hb#xt#\hsize{\hfil\box\#tempboxa\hfil}%
\fi
\vskip\belowcaptionskip}
If you wrap the whole thing in a minipage environment (as suggested to be done manually in the other answers), you should get the results you want.
Is it easier than loading a package? Not really, but it can be instructive.
There's nothing too magical about \caption; it just consults a figure/table number and than formats your text at a font and size that the document class likes. So you can control its width the same way you'd control the width of any text:
Wrap the \caption{...} in a \parbox or a \begin{minipage}...\end{minipage}.

How do I "fake" slanted text in LaTeX?

A font I am using does not have the slanted/oblique variant to it in LaTeX (NB: not italics), and I would like to have slanted text in places.
Is there an easy way to slant text without having to generate entirely new font files and such?
One suggested solution was to do:
\renewcommand{\textsl}[1]{\tikz[baseline=(X.base)] \node[xslant=0.2231153] (X) {#1};}
This works well for one or two words, but tikz nodes don't break across lines, so it's not adequate, for, say, a theorem environment.
Obviously, a quick-and-dirty method will not give exceptional kerning or spacing, but I am not concerned about that. However, a 13 degree shear/slant would be desirable.
You can do this easily in XeLaTeX:
\usepackage{fontspec}
...
\fontspec[ItalicFont=*,ItalicFeatures=FakeSlant]{Minion Pro}
Highly undesirable, however, if there's any chance you can get a real italic.
Update: why undesirable? Because font outlines are not designed to be distorted! Any sort of transformation besides linear scaling in both directions will change the relationship between the inner/outer curves of the letters, effectively going against the wishes of the font designer.
If you want to highlight something in a different font than the roman and not use italic, try something completely different like a harmonising sans serif, for example.
Use a font that does have italics. Standard fonts are best unless you're in marketing.
I use LuaLaTeX, but the following should also work with XeLaTeX. LuaLaTeX, from my experience, is fully backward compatible with good old LaTeX, and it's only marginally slower. And making new, clever macros is a breeze. Anyhow, enough proselytizing.
This is pulled almost directly from one of my documents. Some of it is hackish, yes, but it suits my needs well - and the results are quite respectable.
\usepackage{relsize} % For \texttt definition below
\usepackage{fontspec}
\setmainfont% Minion Pro, not redistributable?
[Ligatures=TeX,
SlantedFont=*,
SlantedFeatures={FakeSlant=0.2},
BoldSlantedFont=* Bold,
BoldSlantedFeatures={FakeSlant=0.2}
]{Minion Pro}
\setsansfont% Linux Biolinum O % SIL Open Font License
[Ligatures=TeX,
Extension=.otf,
BoldFont=fxbb,
ItalicFont=fxbri,
BoldItalicFont=fxbri,
BoldItalicFeatures={FakeBold=1.5}, % Note: This is not currently working in LuaTeX!
SlantedFont=fxbro,
BoldSlantedFont=fxbbo
]{fxbr}
\setmonofont[Ligatures=TeX]{DejaVu Sans Mono} % "Free License" No General Restictions
\makeatletter
\let\old#texttt\texttt
\renewcommand{\texttt}[1]{{\smaller\old#texttt{#1}}}
\makeatother
Here are some macros for shearing, but that will not be what you want either, I guess.
\usepackage{graphicx}
%\hshearbox{vertical_prescale_times_shearfactor}{one_divide_by_shearfactor}{content}
% an initial vertical downscale is often necessary for a 3d projection
\newcommand{\hshearbox}[3]{\scalebox{0.866025}[#2]{\rotatebox{210}%
{\scalebox{1.73205}[-0.57735]{\rotatebox{60}{\scalebox{-1.1547}[#1]{#3}}}}}}
%\vshearbox{horizontal_prescale_times_shearfactor}{one_divide_by_shearfactor}{content}
% an initial horizontal downscale is often necessary for a 3d projection
\newcommand{\vshearbox}[3]{\scalebox{#2}[0.866025]{\rotatebox{210}%
{\scalebox{-0.57735}[1.73205]{\rotatebox{60}{\scalebox{#1}[-1.1547]{#3}}}}}}
The XeLaTeX/fontspec answer mentioned above somehow wouldn't work for me, so I came up with this:
\documentclass{article}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\newcommand{\slsc}[1]{\fontspec[SmallCapsFeatures={FakeSlant=0.6}]{Linux Libertine O}\textsc{#1}\fontspec[]{Linux Libertine O}}
\begin{document}
normaltext
\textsc{textsc}
\slsc{textslsc} % this will produce slanted small caps
\end{document}

Math operator in specifying figure width in LaTeX

In LaTeX figures, one can use \textwidth and \columnwidth to specify sizes of the graphic relative to the size of surrounding text, e.g.
\includegraphics[width=.5\textwidth]{myimage}.
I would like to easily switch from onecolumn to twocolumn template (and back) without the figure growing too large for onecolumn template. For twocolumn template (where \columnwidth is roughly half the \textwidth), I would like to have something like:
\includegraphics[width=.9\columnwidth]{myimage}.
and for onecolumn template (where \columnwidth and \textwidth are equal):
\includegraphics[width=.5\textwidth]{myimage}.
Now, I figured I could limit this using some kind of a min operator:
\includegraphics[width=min(.5\textwidth,.9\columnwidth)]{myimage} but this is invalid syntax. Is there something like this to solve this problem, possibly through the use of LaTeX macro system?
Although it's possible to write this sort of macro, I wouldn't want to hardcode it into each figure; how about something like this
\makeatletter
\newlength \figwidth
\if#twocolumn
\setlength \figwidth {0.9\columnwidth}
\else
\setlength \figwidth {0.5\textwidth}
\fi
\makeatother
and then use
\includegraphics[width=\figwidth]{myimage}
to insert the graphic.
\textwidth is the horizontal width of the page body and not really appropriate for your purposes.
\linewidth is the width of the current line; it will be updated appropriate to columns, indentation, etc.
The following paragraph produces a picture that should precisely fit the entire line width (i.e. no overful warning):
\noindent\includegraphics[width=\linewidth]{myimage}
If you prefer small margins on the left and right, you can use:
\begin{center}
\includegraphics[width=.9\linewidth]{myimage}
\end{center}
Or, if you want to specify the margins in an absolute size:
\usepackage{calc}
...
\begin{center}
\includegraphics[width=\linewidth-20pt]{myimage}
\end{center}
Hmm... the code above (\if#twocolumn etc.) is not working for me at all. No idea why not. :( tetex on osX using fink. Trying to use revtex4, so perhaps that's the problem. I really like the idea of this type of change because I'm going to be dorking with widths etc. for my thesis and various journal articles, and to have these distances specified with a macro may be helpful for these types of conversions.
Any comments greatly appreciated!
-Allen
Somebody else who is more familiar with this will probably answer, but note that you would also need to change your figure type to be figure* if you are going two-column.

Resources