6

Friends: Can anybody tell me why the letter "e" of entropy does not show up in the following snippet?

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation}
\begin{aligned}
\mathcal entropy[9,5] & = \mathcal - [(\frac{9}{14})*lg(\frac{9}{14}) + (\frac{5}{14})*lg(\frac{5}{14})] \\
& = - [(\frac{9}{14})*((lg(9)-lg(14)) + (\frac{5}{14})*((lg(5) - lg(14))] \\
& = - [(\frac{9}{14})*(\frac{log(9)}{log(2)}) - (\frac{log(14)}{log(2)}) + (\frac{5}{14})*(\frac{log(5)}{log(2)}-\frac{log(14}{log(2)})]\\
& = 0.940
\end{aligned}
\end{equation}
\end{document}
Alan Munn
  • 218,180

2 Answers2

11

The following example

  • removes \mathcal that does not seem to have a purpose,
  • fixes parentheses,
  • sets entropy as text word and not as product of variables e, n, t, ...,
  • sets the operators lg and log in upright font,
  • tries to improve the formatting by

    • better sizes of the brackets,
    • removal of unneeded brackets,
    • removal of unneeded operators (*),
    • using package siunitx for formatting the final number.

The example file:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mleftright}
\usepackage{siunitx}
% For text in math, see comments.
\newcommand*{\tx}{\textnormal}
\begin{document}
\begin{equation}
\begin{aligned}
\tx{entropy}[9,5]
& = - \mleft[\frac{9}{14}\lg\Bigl(\frac{9}{14}\Bigr)
  + \frac{5}{14}\lg\Bigl(\frac{5}{14}\Bigr)\mright] \\
& = -
  \mleft[
    \frac{9}{14}\bigl(\lg(9)-\lg(14)\bigr)
    +
    \frac{5}{14}\bigl(\lg(5) - \lg(14)\bigr)
  \mright] \\
& = -
  \mleft[
    \frac{9}{14} \left(\frac{\log(9)}{\log(2)} - \frac{\log(14)}{\log(2)}\right)
    +
    \frac{5}{14} \left(\frac{\log(5)}{\log(2)}-\frac{\log(14)}{\log(2)}\right)
  \mright]\\
& \approx \num{0.9402859586}
\end{aligned}
\end{equation}
\end{document}

Result

The formula between the \left and \right brackets are put in a "inner" math formula. This can cause additional horizontal space around the formula, for example, there is a thin space between an unary minus symbol and the opening bracket. \mleft and \mright of package mleftright avoids this.

Heiko Oberdiek
  • 271,626
  • 1
    If you change all instances of \left[ and \right] to \biggl[ and \biggr], respectively, TeX will have a chance to treat the initiai - symbols as a unary rather than as a binary operators, by reducing the amount of whitespace between - and \biggl[. – Mico Apr 28 '18 at 20:06
  • 3
    @Mico The minus sign after a relation symbol can never be a binary math atom. The space (\thinmuskip) is added between the unary minus sign as ordinary math atom and the inner math formula by \left and \right. I have updated the answer to use \mleft and \mright of package mleftright instead. – Heiko Oberdiek Apr 28 '18 at 20:49
  • Probably \mathrm{entropy} is better. – egreg Apr 28 '18 at 22:26
  • @egreg Unhappily, there is no "best" way, just different ways with more or less advantages and disadvantages depending on the current situation. More details, e.g.: Write 'text' correctly in equations – Heiko Oberdiek Apr 28 '18 at 22:53
  • @HeikoOberdiek Sorry, but \text is wrong anyway. If the equation is part of a theorem statement, “entropy” would be typeset in italics. – egreg Apr 28 '18 at 23:01
  • 1
    @egreg \mathrm is also wrong, it fails for text with text accents, for example. For my taste, using italics in theorems is horrible. Depending on the font settings, \textnormal is often a good compromise, but the name is lengthy. The best is probably a user macro that can be defined depending on the use cases, formatting requirements, and taste. – Heiko Oberdiek Apr 28 '18 at 23:05
10

\mathcal is a macro with an argument. In your example, because of lack of {...}, the argument is e. But \mathcal works only with capital letters, hence the side effect.

Additionally, \mathcal is typically used to denote families of sets. It seems that you wanted some other effect.

  • Thanks, I just wanted to say entropy = math expression and continue on the next line but only working at the right hand side of the equal sign. There are about 5 lines of simple math manipulation. I am newbie in LATEX. What do you mean by (...)? – Ramon A. Mata-Toledo Apr 28 '18 at 18:14
  • @RamonA.Mata-Toledo {...} was for an argument, e.g. \matcal{Z}. – Przemysław Scherwentke Apr 28 '18 at 19:13
  • In unicode-math, \mathcal is defined for small Latin letters. U+212E is the Unicode character for script small letter e. The legacy stix package defines a \mathscr{e}. Agreed that it’s being used in error here, however. – Davislor Apr 28 '18 at 21:49
  • "\mathcal is typically used to...". Well, no, people use it for many other types of objects. I agree that in this case it is a slightly weird choice, though. – Pedro Apr 29 '18 at 13:57