Search code examples
latexbeamer

Scale an equation* to fit exact page width


I have a beamer presentation with lots of equations that are too big for a line.
I want to automatically scale them to fit.
The solution to Scale an equation to fit exact page width by https://stackoverflow.com/users/3383640/suuuehgi was great.

However I do not want equation numbers - I want this same resizing to apply to all equation* bits.
I can't figure out how to adjust the code to deal with this. Help much appreciated!


Solution

  • The following code uses a slightly improved approach, noting that content within a display equation is set using \displaystyle (not contained within the measurement in the linked answer). In principle, the approach is the same:

    • The content is measured horizontally.
    • The content is resized to fit if it is wider than \linewidth.

    The equation* shorthand is redefined to also accommodate this new approach.

    enter image description here

    \documentclass{beamer}
    
    \usepackage{environ}
    
    \newlength{\myl}
    \expandafter\let\expandafter\origequation\csname equation*\endcsname
    \expandafter\let\expandafter\endorigequation\csname endequation*\endcsname
    \long\def\[#1\]{\begin{equation*}#1\end{equation*}}
    \RenewEnviron{equation*}{
      \settowidth{\myl}{$\displaystyle\BODY$} % calculate width and save as \myl
      \origequation
        \ifdim\myl>\linewidth
          \resizebox{\linewidth}{!}{$\displaystyle\BODY$}% \myl > \linewidth
        \else
          \BODY % \myl <= \linewidth
        \fi
      \endorigequation
    }
    
    \begin{document}
    
    \begin{frame}
      \begin{equation*}
        f(x) = ax^2 + bx + c
      \end{equation*}
      
      \begin{equation*}
        g(x) = ax^2 + bx + c 
          - ax^2 - bx - c 
          + ax^2 + bx + c
          - ax^2 - bx - c
          + ax^2 + bx + c
      \end{equation*}
      
      \[
        h(x) = \int_a^b \frac{c}{d} x\,\mathrm{d}x
      \]
      
      \[
        i(x) = \int_a^b \frac{c}{d} x\,\mathrm{d}x
          - \int_a^b \frac{c}{d} x\,\mathrm{d}x
          + \int_a^b \frac{c}{d} x\,\mathrm{d}x
          - \int_a^b \frac{c}{d} x\,\mathrm{d}x
          + \int_a^b \frac{c}{d} x\,\mathrm{d}x
          - \int_a^b \frac{c}{d} x\,\mathrm{d}x
          + \int_a^b \frac{c}{d} x\,\mathrm{d}x
      \]
    \end{frame}
    
    \end{document}