9

If I have a document like this:

\documentclass[landscape, 12pt]{report}

\usepackage[landscape]{geometry}

\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{amsmath, amssymb, graphics, setspace}
\newcommand{\mathsym}[1]{{}}
\newcommand{\unicode}[1]{{}}
\newcounter{mathematicapage}

\begin{document}    \begin{equation}\label{Equation:Naive_Bayes_Classifier}
    P\left(H_h|E_1,E_2,\ldots ,E_e,\ldots    E_{\mathbb{E}}\right)=\frac{P\left(H_h\right) P\left(E_1|H_h\right)    P\left(E_2|H_h,E_1\right) \text{$\ldots $P}
    \left(E_e|H_h,E_1,E_2,\ldots ,E_{e-1},E_{e+1} \ldots, E_{\mathbb{E}}\right) \text{$\ldots    $P}
    \left(E_{\mathbb{E}}|H_h,E_1,E_2,\ldots ,E_{\mathbb{E}-1}\right)}{ P\left(E_1,E_2,\ldots,E_e, \ldots    ,E_{\mathbb{E}}\right)}
\end{equation}
\end{document}

How can I auto adjust this equation to appear in the entire page, using all horizontal space? (Without badboxes or smaller than the width of the page?)

If is there a kind of scale to use in equations, giving explicity how greater the equation should become, this solver my problem too.

7
  • I added a }{ to get your code to work- let me know if it wasn't appropriate (or simply roll back or edit)
    – cmhughes
    Nov 21 '11 at 2:20
  • 1
    You may also consider adjusting the equation layout (and not merely using \frac) in order to make it fit within \textwidth.
    – Werner
    Nov 21 '11 at 4:08
  • Why those useless \left and \right? And \text{$\ldots$ P} doesn't make much sense. If you want an upright P it should be \mathrm{P} in all the formula.
    – egreg
    Nov 21 '11 at 14:52
  • The equation was generated in Mathematica, which makes a ton of warts like that.
    – Chel
    Nov 21 '11 at 15:53
  • @rdhs is correct.
    – GarouDan
    Nov 21 '11 at 19:38
15

To "scale" an equation to fit a box you can use \resizebox from the package graphicx. Below is a stripped example of your code that does what you are looking for.

\documentclass[landscape, 12pt]{report}
\usepackage{graphicx}
\pagestyle{empty}

\begin{document}
\noindent Here is some text
\begin{equation}
\resizebox{.9 \textwidth}{!} 
{
    $ a + b $
}
\end{equation}
and here is some more.
\end{document}

Here the .9 determines how much of the width you'd like to take up, in this case I choose 90% though you can fit to your liking. The ! as the second argument will preserve the aspect ratio.

Giving credit where it is due, a very similar question was answered on Stack Exchange recently about shrinking the equation. In both cases the \resizebox should suffice, though it is not recommended. Personally, I would consider splitting the equation. Having an inconsistent math font size can be a typographical nightmare.

Examples

Here the argument to \resizebox is set to 1.0 \textwidth (compiled with pdflatex):

Example 1

In this example it is set to 0.2 \textwidth

Example 2

6
  • Your solution looks like a good one. But I tried implement it in kile and the a+b have the same size, even changing the .9 to .1, .99, 5.5. What's going on?
    – GarouDan
    Nov 21 '11 at 20:16
  • @GarouDan Hard to tell without seeing what you are seeing. I've added a picture so you can see what the final result looks like, I've slightly modified the code by adding some outside text so you can get a feel for how the equation scales.
    – Hooked
    Nov 21 '11 at 20:30
  • This is very strange. Using this code I got this image outup in my dvi. I'm using kile and using ubuntu 10.04 lts =// if helps.
    – GarouDan
    Nov 21 '11 at 20:53
  • @GarouDan try compiling it with pdflatex from the command line, that's how the examples above were produced.
    – Hooked
    Nov 21 '11 at 21:02
  • lol @Hooked. Using pdf (pdflatex) works (I usually never uses pdf, sorry). Why not with dvi (I use latex and then see the dvi)? Thanks, I think this ends the question.
    – GarouDan
    Nov 21 '11 at 21:18
6

Breaking huge fractions certainly is tricky, and I don't think there exists an automated solution for that.

So first off, let's see how your formula looks like (examples in plain-format):

\input amssym.def
\def\P{{\rm P}}
\def\E{\Bbb E}
$$
  \P(H_h|E_1,E_2,\dots,E_e,\dots E_\E)
  = { \P(H_h) \P(E_1|H_h) \P(E_2|H_h,E_1) \dots
    \P(E_e|H_h,E_1,E_2,\dots,E_{e-1},E_{e+1}\dots,E_\E) \ldots
    \P(E_\E|H_h,E_1,E_2,\dots,E_\E-1)
    \over
    \P(E_1,E_2,\dots,E_e,\dots,E_\E) }
$$

enter image description here

Oh my, we ran out of paper!

Knuth gives an example in the TeXbook how you can break the formula (exercise 19.9), and applying that solution here isn't exactly straight-forward, for there is many ways to do the splitting. Let's start off with two-line split:

$$
  \P(H_h|E_1,E_2,\dots,E_e,\dots E_\E)
  = {\displaystyle{\P(H_h) \P(E_1|H_h) \P(E_2|H_h,E_1) \dots
    \P(E_e|H_h,E_1,E_2,\dots,E_{e-1},E_{e+1}\dots,E_\E) \ldots
    \atop
    \hfill\P(E_\E|H_h,E_1,E_2,\dots,E_\E-1) } \over
      \P(E_1,E_2,\dots,E_e,\dots,E_\E) }
$$

enter image description here

Well, we didn't run out of paper, but we were left with an overfull box.

Maybe if we introduced a not-so-natural break instead:

$$
  \P(H_h|E_1,E_2,\dots,E_e,\dots E_\E)
  = { \displaystyle{ \P(H_h) \P(E_1|H_h) \P(E_2|H_h,E_1) \dots
    \P(E_e|H_h,E_1,E_2,
    \hfill
    \atop
    \qquad \dots,E_{e-1},E_{e+1}\dots,E_\E) \ldots
    \P(E_\E|H_h,E_1,E_2,\dots,E_\E-1) }
    \over
    \P(E_1,E_2,\dots,E_e,\dots,E_\E) }
$$

enter image description here

Now the space-requirements are better, albeit with a cost.

You could go really over the board and split it into three lines:

$$
  \P(H_h|E_1,E_2,\dots,E_e,\dots E_\E)
  = { \displaystyle{
      \displaystyle{ \P(H_h) \P(E_1|H_h) \P(E_2|H_h,E_1) \dots \hfill
      \atop
      \qquad \P(E_e|H_h,E_1,E_2,\dots,E_{e-1},E_{e+1}\dots,E_\E) \ldots }
      \atop
      \hfill \P(E_\E|H_h,E_1,E_2,\dots,E_\E-1) }
    \over
    \P(E_1,E_2,\dots,E_e,\dots,E_\E) }
$$

enter image description here

But that might be a tad too funky. I dunno.

So, how would an automated solution choose? I guess it would be really tricky.

2
  • 1
    Another option is to set the RHS as \frac {1}{P(E_1,...E_{E}} \Big[ P(H_h) ... \Big]. Then the term in the brackets can be split into multiple lines using aligned or multlined.
    – Aditya
    Nov 22 '11 at 0:21
  • Not sure how much this matters, but you may want to replace E_\E-1 with E_{\E-1} (towards the very end of the long numerator expressions) in the various examples you provide.
    – Mico
    Dec 22 '12 at 16:26
6

Package resizegather can automatically shrink equations of environment gather of package amsmath to fit the line width.

The following example compares the equation using \textstyle of rdhs' answer with resizing the overlarge equation (too large by 148.64104pt):

\documentclass[landscape, 12pt]{report}
\usepackage[landscape]{geometry}
\usepackage{amsmath,amssymb}

\usepackage{resizegather}

\begin{document}
\begin{gather}
  \label{eq-tfrac}
    P\left(H_h|E_1,E_2,\ldots ,E_e,\ldots    E_{\mathbb{E}}\right)=\tfrac{P\left(H_h\right) P\left(E_1|H_h\right)    P\left(E_2|H_h,E_1\right) \text{$\ldots $P}
    \left(E_e|H_h,E_1,E_2,\ldots ,E_{e-1},E_{e+1} \ldots, E_{\mathbb{E}}\right) \text{$\ldots    $P}
    \left(E_{\mathbb{E}}|H_h,E_1,E_2,\ldots ,E_{\mathbb{E}-1}\right)}{ P\left(E_1,E_2,\ldots,E_e, \ldots    ,E_{\mathbb{E}}\right)}
  \\\label{eq-resized}
    P\left(H_h|E_1,E_2,\ldots ,E_e,\ldots    E_{\mathbb{E}}\right)=\frac{P\left(H_h\right) P\left(E_1|H_h\right)    P\left(E_2|H_h,E_1\right) \text{$\ldots $P}
    \left(E_e|H_h,E_1,E_2,\ldots ,E_{e-1},E_{e+1} \ldots, E_{\mathbb{E}}\right) \text{$\ldots    $P}
    \left(E_{\mathbb{E}}|H_h,E_1,E_2,\ldots ,E_{\mathbb{E}-1}\right)}{ P\left(E_1,E_2,\ldots,E_e, \ldots    ,E_{\mathbb{E}}\right)}
\end{gather}
\end{document}

Result

Including the equation number on the same line

Two examles/variations:

\documentclass[landscape, 12pt]{report}
\usepackage[landscape]{geometry}
\usepackage{amsmath,amssymb}

\usepackage{graphicx}
\usepackage{tabularx}

\newcommand*{\Sample}{%
  P\left(H_h|E_1,E_2,\ldots ,E_e,\ldots
  E_{\mathbb{E}}\right)=\frac{P\left(H_h\right) P\left(E_1|H_h\right)
  P\left(E_2|H_h,E_1\right) \text{$\ldots $P}
  \left(E_e|H_h,E_1,E_2,\ldots ,E_{e-1},E_{e+1} \ldots,
  E_{\mathbb{E}}\right) \text{$\ldots    $P}
  \left(E_{\mathbb{E}}|H_h,E_1,E_2,\ldots ,
  E_{\mathbb{E}-1}\right)}{ P\left(E_1,E_2,\ldots,E_e, \ldots
  ,E_{\mathbb{E}}\right)}%
}

\begin{document}
\refstepcounter{equation}
\[
  \resizebox{\linewidth}{!}{$\displaystyle\Sample$\quad(\theequation)}
\]
%
\refstepcounter{equation}
\[
  \begin{tabularx}{\linewidth}{@{}X@{\quad}l@{}}
    \resizebox{\linewidth}{!}{$\displaystyle\Sample$}&(\theequation)
  \end{tabularx}
\]
\end{document}

Result

Note: Keep in mind that it is usually better to avoid shrunk oversized equations.

3
  • Brilliant solution. I was looking for something like this for long time. Thanks
    – Nasser
    Mar 22 '18 at 0:39
  • Is it possible to force the numbering to be on the right side (1st equation) instead of at the bottom (2nd equation)? I am trying to use gather with multiple large equations and would prefer to have the number on the right side even if the equations would become a little smaller. Jun 21 '18 at 15:17
  • 1
    @GuilhermeSalomé See updated answer. Jun 21 '18 at 17:00
3

Here's a solution using the mathtools package and its \splitfrac command; note that the \splitfrac directives may be nested. Overall, this solution looks very much like the third solution provided earlier by @morbusg. The advantage of the new solution may lie in its not having to use the TeX "primitive" command \atop.

Observe that there's no longer a need to typeset the formula in landscape mode. In addition, I've replaced some of the \ldots with \cdots, so as to better distinguish between items in a simple math list and items in a multiplicative list. Moreover, I've replaced the | vertical bars with \mid directives; the latter provide better horizontal spacing. Finally, I've gotten rid of the \left and \right directives, as they actually don't do anything in terms of resizing the parentheses in the present example.

enter image description here

\documentclass{report}
\usepackage[margin=1in]{geometry}
\usepackage{mathtools, amssymb}

\begin{document} 
\begin{equation}\label{Equation:Naive_Bayes_Classifier}
P(H_h\mid E_1,E_2,\dots ,E_e,\dots E_{\mathbb{E}})=
\frac{\left( % place large parens around entire numerator expression
\splitfrac{P(H_h)\, P(E_1\mid H_h) \,
  P(E_2\mid H_h,E_1) \cdots }{
  \splitfrac{
    P(E_e\mid H_h,E_1,E_2,\dots ,E_{e-1},E_{e+1} \dots, 
      E_{\mathbb{E}}) \cdots }{
    P(E_{\mathbb{E}}\mid H_h,E_1,E_2,\dots,
      E_{\mathbb{E}-1})
  } % end of inner splitfrac
}   % end of outer splitfrac
\right)}
{ P(E_1,E_2,\dots,E_e,\dots,E_{\mathbb{E}})}
\end{equation}
\end{document}

If you want a bit more vertical whitespace between the three lines of the numerator expression, you could replace the \splitfrac directives with \splitdfrac instructions.

3

You could make the fraction smaller using \textstyle:

\begin{equation}
  P(H_h|E_1,E_2,\ldots ,E_e,\ldots E_{\mathbb{E}})
  =\textstyle\frac{P(H_h) P(E_1|H_h)
  P(E_2|H_h,E_1) \text{$\ldots $P}
  (E_e|H_h,E_1,E_2,\ldots ,E_{e-1},E_{e+1} \ldots,E_{\mathbb{E}})\text{$\ldots$P}
  (E_{\mathbb{E}}|H_h,E_1,E_2,\ldots ,E_{\mathbb{E}-1})}
  { P(E_1,E_2,\ldots,E_e, \ldots ,E_{\mathbb{E}})}
\end{equation}

Or you could use \\ within the amsmath environment multline to introduce a line-break:

\begin{multline}
  P(H_h|E_1,E_2,\ldots ,E_e,\ldots E_{\mathbb{E}})=\\
  \frac{P(H_h) P(E_1|H_h)
  P(E_2|H_h,E_1) \text{$\ldots $P}
  (E_e|H_h,E_1,E_2,\ldots ,E_{e-1},E_{e+1} \ldots,E_{\mathbb{E}})\text{$\ldots$P}
  (E_{\mathbb{E}}|H_h,E_1,E_2,\ldots ,E_{\mathbb{E}-1})}
  { P(E_1,E_2,\ldots,E_e, \ldots ,E_{\mathbb{E}})}
\end{multline}

Of course, it looks nicer with the equals sign on the same line as the fraction, but your margins are too big for that.

With the line-break moved, the equation becomes:

\begin{multline}
  P(H_h|E_1,E_2,\ldots ,E_e,\ldots E_{\mathbb{E}})\\
  =\frac{P(H_h) P(E_1|H_h)
  P(E_2|H_h,E_1) \text{$\ldots $P}
  (E_e|H_h,E_1,E_2,\ldots ,E_{e-1},E_{e+1} \ldots,E_{\mathbb{E}})\text{$\ldots$P}
  (E_{\mathbb{E}}|H_h,E_1,E_2,\ldots ,E_{\mathbb{E}-1})}
  { P(E_1,E_2,\ldots,E_e, \ldots ,E_{\mathbb{E}})}
\end{multline}

And then you can change the page margins using the geometry package options:

\usepackage[margin=1in,landscape]{geometry}
2
  • This is really helpfull @rdhs. Thanks so much. I need this this equations as large as possible to cut and use as a image in my presentation.
    – GarouDan
    Nov 21 '11 at 13:17
  • Unfortunally I need something more. Something like how change the size of a equation? Is there a scale to equations who I can explicity change the size in inches or points or cm's?
    – GarouDan
    Nov 21 '11 at 14:23
1

Okay, your comment really clarifies what you want, and there's a better solution for that. (I'm not quite sure if it's appropriate to post a separate answer, but here goes.)

If you want the page to grow to fit the equation, then the best solution is to use the standalone document class and the varwidth package. You can also remove the equation numbering:

\documentclass[12pt]{standalone}
\usepackage{varwidth}

\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{amsmath, amssymb, graphics, setspace}
\newcommand{\mathsym}[1]{{}}
\newcommand{\unicode}[1]{{}}
\newcounter{mathematicapage}

\begin{document}
\begin{varwidth}{50in}
\begin{equation*}
  P(H_h|E_1,E_2,\ldots ,E_e,\ldots E_{\mathbb{E}})
  =\frac{P(H_h) P(E_1|H_h)
  P(E_2|H_h,E_1) \text{$\ldots $P}
  (E_e|H_h,E_1,E_2,\ldots ,E_{e-1},E_{e+1} \ldots,E_{\mathbb{E}})\text{$\ldots$P}
  (E_{\mathbb{E}}|H_h,E_1,E_2,\ldots ,E_{\mathbb{E}-1})}
  { P(E_1,E_2,\ldots,E_e, \ldots ,E_{\mathbb{E}})}
\end{equation*}
\end{varwidth}
\end{document}

This gives a PDF file that can be converted directly to PNG and put into slides, no cropping required. You could probably get Mathematica to change its output like this automatically, but if not you could use sed to automate the substitutions.

4
  • When I tryed to run your code in Kile (Ubuntu 10.04 LTS) I got, File 'standalone.cls' not found. \usepackage. Where I can get this package (no result in google yet =/)?
    – GarouDan
    Nov 21 '11 at 14:42
  • 1
    Here's a standalone.cls: tug.org. And you can scale the page however you want. If you include it in another LaTeX doc (e.g., beamer slides), do something like \includegraphics[width=8 in]{bigeqn.pdf}. If you convert it to a PNG using GraphicsMagick, so something like gm convert -monochrome -geometry 4800x -density 600 bigeqn.pdf bigeqn.png (4800 = 8 in x 600 ppi).
    – Chel
    Nov 21 '11 at 14:47
  • Sorry, I couldn't solve the problem yet. Please see this code in pastebin. (I have installed standalone package)I tried several things but doesn't worked very well. Can you write a new code for me?
    – GarouDan
    Nov 21 '11 at 15:30
  • Thanks you so much. Now, with Hooked help (using pdf's instead dvi) I can see your idea works fine. Really thanks. If I could I'd like accept your solution as answer too. So, if someone like this solution as I please vote up. Cya.
    – GarouDan
    Nov 21 '11 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.