# How to resize font of displayed formulas to automatically fit column width? [duplicate]

Is there a way, in LaTeX, to automatically resize font of displayed equations to fit column width (241 pt, here)?

Any ideas would be appreciated.

You can see my equation below; note that I use \fontsize{4pt}{4pt}\selectfont to resize the font, but I need the font to be resized automatically every time.

{\fontsize{4pt}{4pt}\selectfont{\begin{gather}\begin{gathered}{\varphi
}_{ij}^{q}\left(t\right)=2\left[\dfrac{{S}_{a}}{{L}_{a}^{2}}{e}^{-({\Vert
{x}_{j}(t)-{x}_{i}(t)-{\Delta }_{ij}^{q}\Vert
}^{2}/{L}_{a}^{2})}-\dfrac{{S}_{r}}{{L}_{r}^{2}}{e}^{-({\Vert
{x}_{j}(t)-{x}_{i}(t)-{\Delta }_{ij}^{q}\Vert
}^{2}/{L}_{r}^{2})}+\left(\dfrac{{S}_{r}}{{L}_{r}^{2}}+\dfrac{{S}_{r}}{{L}_{a}^{2}}\right){e}^{(-1/{L}_{r}^{2}+1/{L}_{a}^{2})({\Vert
{x}_{j}(t)-{x}_{i}(t)-{\Delta }_{ij}^{q}\Vert
}^{2})}\right].\label{eq6}\end{gathered}\end{gather}}}

-

## marked as duplicate by Peter Jansson, Jesse, Heiko Oberdiek, Svend Tveskæg, ChrisSFeb 23 '14 at 10:03

Welcome to TeX.SX! Use 241pt as the width argument of the \resizebox as described in the accepted answer to the linked question. – Paul Gessler Feb 23 '14 at 8:05
Thanks @PaulGessler but when i use \resizebox it did not work i wrote it as \resizebox{\textwidth}{!}{<content>} ? – MohamedSayed Feb 23 '14 at 8:26

The adjustbox package is handy. You put the contents inside adjustbox environment as

\begin{adjustbox}{max width=241pt}
contents


then if the contents are wider than 241pt, then only they are resized otherwise not.

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\noindent A\hfill Z
$$\begin{adjustbox}{max width=241pt}  \varphi_{ij}^q(t) = 2\left[ \dfrac{S_a}{L_a^2} e^{ -(\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2/L_a^2)} - \dfrac{S_r}{L_r^2} e^{ -(\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2/L_r^2)} + \left( \dfrac{S_r}{L_r^2} + \dfrac{S_r}{L_a^2} \right) e^{(- 1/L_r^2 + 1/L_a^2) (\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2)} \right] \label{eq6}  \end{adjustbox}$$
\end{document}


-

Package resizegather can resize equations, especially the equations from environment gather of package amsmath.

But tiny font sizes are difficult to read, thus I have added a version with line breaks:

\documentclass{article}
\usepackage{amsmath}
\usepackage{resizegather}

\begin{document}
\noindent A\hfill Z
\begin{gather}
\varphi_{ij}^q(t) =
2\left[
\dfrac{S_a}{L_a^2}
e^{ -(\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2/L_a^2)}
- \dfrac{S_r}{L_r^2}
e^{ -(\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2/L_r^2)}
+ \left( \dfrac{S_r}{L_r^2} + \dfrac{S_r}{L_a^2} \right)
e^{(- 1/L_r^2 + 1/L_a^2)
(\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2)}
\right]
\label{eq6}\\
\begin{aligned}
\varphi_{ij}^q(t) =
2\bigg[&
\dfrac{S_a}{L_a^2}
e^{ -(\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2/L_a^2)}
\\&
- \dfrac{S_r}{L_r^2}
e^{ -(\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2/L_r^2)}
\\&
+ \left( \dfrac{S_r}{L_r^2} + \dfrac{S_r}{L_a^2} \right)
e^{(- 1/L_r^2 + 1/L_a^2)
(\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2)}
\bigg]
\label{eq7}
\end{aligned}
\end{gather}
\end{document}


## \setlength{\textwidth}{241pt}

In such a narrow column resizegather has to scale both equations down:

Package resizegather Warning: Equation line 1 is too large by 267.77846pt
(resizegather)                in environment gather' on input line 35.

Package resizegather Warning: Equation line 2 is too large by 20.76895pt
(resizegather)                in environment gather' on input line 35.


Thus I moved the second and third line in the second equation a little to the left:

\documentclass{article}
\usepackage{amsmath}
\usepackage{resizegather}

\setlength{\textwidth}{241pt}

\begin{document}
\noindent A\hfill Z
\begin{gather}
\varphi_{ij}^q(t) =
2\left[
\dfrac{S_a}{L_a^2}
e^{ -(\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2/L_a^2)}
- \dfrac{S_r}{L_r^2}
e^{ -(\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2/L_r^2)}
+ \left( \dfrac{S_r}{L_r^2} + \dfrac{S_r}{L_a^2} \right)
e^{(- 1/L_r^2 + 1/L_a^2)
(\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2)}
\right]
\label{eq6}\\
\begin{split}
\varphi_{ij}^q(t) &=
2\bigg[
\dfrac{S_a}{L_a^2}
e^{ -(\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2/L_a^2)}
\\&
- \dfrac{S_r}{L_r^2}
e^{ -(\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2/L_r^2)}
\\&
+ \left( \dfrac{S_r}{L_r^2} + \dfrac{S_r}{L_a^2} \right)
e^{(- 1/L_r^2 + 1/L_a^2)
(\Vert x_j(t) - x_i(t) - \Delta_{ij}^q\Vert^2)}
\bigg]
\label{eq7}
\end{split}
\end{gather}
\end{document}


-
You may want to add the instruction \setlength{\textwidth}{241pt} to the MWE to illustrate just how tiny the equation would look when it's set on one line. – Mico Feb 23 '14 at 9:07
@Mico: And the subscripts might even be printed as single dots on some printers. – Heiko Oberdiek Feb 23 '14 at 9:28

Expanding on my comment: you need to use \resizebox{241pt}{!}{<content>}. Drop the gather and gathered environments because you're not using any of their features.

Here is the code:

\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}

\begin{document}
$$\resizebox{241pt}{!}{% {\varphi}_{ij}^{q}\left(t\right)=2\left[% \dfrac{{S}_{a}}{{L}_{a}^{2}}{e}^{-({\Vert{x}_{j}(t)-{x}_{i}(t)-{\Delta }_{ij}^{q}\Vert}^{2}/{L}_{a}^{2})} - % \dfrac{{S}_{r}}{{L}_{r}^{2}}{e}^{-({\Vert{x}_{j}(t)-{x}_{i}(t)-{\Delta }_{ij}^{q}\Vert}^{2}/{L}_{r}^{2})} + % \left(\dfrac{{S}_{r}}{{L}_{r}^{2}}+\dfrac{{S}_{r}}{{L}_{a}^{2}}\right){e}^{(-1/{L}_{r}^{2}+1/{L}_{a}^{2})% ({\Vert{x}_{j}(t)-{x}_{i}(t)-{\Delta }_{ij}^{q}\Vert}^{2})}% \right].% \label{eq6}% }$$
\centering
\rule{241pt}{0.5pt} % just to show width

The above rule is 241\,pt wide and is the same width as Eq.~\ref{eq6}.
\end{document}


And the output:

-
Thank you guys it was very helpful answers and many thanks to @HeikoOberdiek your answer was the best to my needs thank you – MohamedSayed Feb 23 '14 at 9:05

First off: Whatever you do, do not use a font size of 4pt -- or even smaller. Doing so will almost certainly guarantee that nobody will be able to decipher the tiny characters, let alone understand what you're trying to say.

Your equation could be broken automatically with the breqn package if the text block were a bit wider. However, given that you have a fairly narrow text block (241pt wide, right?), I think you need to provide your own line breaking instructions in order to have a chance of generating an equation that's typographically appealing. The code below uses the split environment inside an equation environment to do so. It also defines a macro called \norm to simplify some of the code.

\documentclass{article}
\setlength{\textwidth}{241pt}
\usepackage{mathtools,lmodern}
\DeclarePairedDelimiter{\norm}{\lVert}{\rVert}
\begin{document}\pagestyle{empty}
$$\begin{split} \varphi_{ij}^{q}(t)= 2\biggl[&\frac{S_a}{L_a^2} e^{[-(1/L_a^2)\norm{x_j(t)-x_i(t)-\Delta_{ij}^{q}}^2]}\\ -&\frac{S_r}{L_r^2} e^{[-(1/L_r^2)\norm{x_j(t)-x_i(t)-\Delta_{ij}^{q}}^2]}\\ +\biggl(&\frac{S_r}{L_r^2}+\frac{S_r}{L_a^2}\biggr) e^{[(-1/L_r^2+1/L_a^2) \norm{x_j(t)-x_i(t)-\Delta_{ij}^{q}}^2]} \biggr]. \end{split}$$
\hrule % just to illustrate width of text block
\end{document}

-