0

I have written the following latex


\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{amsmath}
\renewcommand{\P}{\mathcal{P}}
\date{May 2019}

\begin{document}

\begin{align*}
    &H_{n + 1}= \\
    &-\sum_{x_{n + 1}} \P(x_{n + 1} | x_n) \sum_{x_1, x_2, \dots, x_{n}} \P(x_1, x_2, \dots, x_n) \log(\P(x_1, x_2, \dots, x_n)\P(x_{n + 1} | x_n)) =\\
    &-\sum_{x_{n + 1}} \P(x_{n + 1} | x_n) \sum_{x_1, x_2, \dots, x_{n}} \P(x_1, x_2, \dots, x_n) (\log(\P(x_1, x_2, \dots, x_n)) + \log(\P(x_{n + 1} | x_n))) =\\
    &-\sum_{x_{n + 1}} \P(x_{n + 1} | x_n) H_n - \sum_{x_1, x_2, \dots, x_{n + 1}} \P(x_1, x_2, \dots, x_{n + 1}) \log(\P(x_{n + 1} | x_n))) =
\end{align*}

\end{document}

This gives me an overfull hbox warning. Is there a way to automatically shrink each line so that it fits inside the hbox?

Note: I am willing to accept a solution that tells me how to shrink everything in the align contents, but I would greatly prefer an answer that automatically shrinks each line dynamically, and I would love an answer that explains how to do both.

6
  • Which \hbox exactly?
    – Bernard
    Commented May 17, 2019 at 22:21
  • @Bernard The full error message is "Overfull \hbox (381.39438pt too wide) in paragraph at lines 69--69". Line 69 is the line containing \end{align*} Commented May 17, 2019 at 22:27
  • The & at the beginning of the first line is automatically forcing everything to the right, though not by very much. The multline* environment of amsmath would be more appropriate, although it still won't reduce the width of the second line. It's not clear what \P is defined to be, and it's not possible to know what the output is without knowing that. Please make your example a complete compilable example, starting with \documentclass and ending with \end{document}; that way potential helpers will have something to experiment with. Commented May 17, 2019 at 22:27
  • This code produces only an ‘Overfull \hbox(4.46786pt)’, which is easy to solve with mathtools. For more than 381pt, there must be something else.
    – Bernard
    Commented May 17, 2019 at 22:33
  • @Bernard Sorry I pasted incomplete latex originally, I edited the question Commented May 17, 2019 at 22:36

1 Answer 1

2

The simplest solution would be to load the geometry package, to have more sensible margins and use the \smashoperator command from mathtools (needless to load amsmath in this case). Without geometry, you'll have to break the second row, for instance with multlined:

\documentclass{article}
\usepackage{showframe}
\renewcommand*{\ShowFrameLinethickness}{0.3pt}
\usepackage[utf8]{inputenc}%
\usepackage{mathtools}

\renewcommand{\P}{\mathcal{P}}
\date{May 2019}

\begin{document}

\begin{align*}
    &H_{n + 1}= \\
    &-\sum_{x_{n + 1}} \P(x_{n + 1}\mid x_n) \smashoperator{\sum_{x_1, x_2, \dots, x_{n}}} \P(x_1, x_2, \dots, x_n) \log(\P(x_1, x_2, \dots, x_n)\P(x_{n + 1} \mid x_n)) =\\
    &-\begin{multlined}\smash[b]{\sum_{x_{n + 1}}} \P(x_{n + 1}\mid x_n) \smash[b]{\smashoperator{\sum_{x_1, x_2, \dots, x_{n}}}} \P(x_1, x_2, \dots, x_n)\bigl (\log(\P(x_1, x_2, \dots, x_n)) \\+ \log(\P(x_{n + 1}\mid x_n))\bigr) =
    \end{multlined}\\
    &-\sum_{x_{n + 1}} \P(x_{n + 1}\mid x_n) H_n -\smashoperator{ \sum_{x_1, x_2, \dots, x_{n + 1}}} \P(x_1, x_2, \dots, x_{n + 1}) \log(\P(x_{n + 1}\mid x_n))) =
\end{align*}

\end{document}

Unrelated: I replaced the occurrences of | in probabilities with \mid to have a better horizontal spacing.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .