5

So I'm writing a latex document that has the following code in it

\begin{align*}
    n! - k &= n! - \sum_{i=1}^{\left\lfloor\frac{n}{r}\right\rfloor}(-1)^{i+1}\binom{n}{r,\dots,n-ir} ((r-1)!)^i(n-ir)!\frac{1}{i!}\\[1em]
    &= n! - \sum_{i=1}^{\left\lfloor\frac{n}{r}\right\rfloor}(-1)^{i+1} \frac{n!}{(r!)^i(n-ir)!}((r-1)!)^i(n-ir)!\frac{1}{i!}\\[1em]
    &= n! - \sum_{i=1}^{\left\lfloor\frac{n}{r}\right\rfloor}(-1)^{i+1} \frac{n!}{r^i(n-ir)!}(n-ir)!\frac{1}{i!}\\[1em]
    &= n! - \sum_{i=1}^{\left\lfloor\frac{n}{r}\right\rfloor}(-1)^{i+1}\frac{n!}{r^i*i!}\\[1em]
    &= n!\left(1-\sum_{i=1}^{\left\lfloor\frac{n}{r}\right\rfloor}(-1)^{i+1}\frac{1}{r^i*i!}\right)\\[1em]
    &= n!\left((-1)^0\frac{1}{r^0*0!}-\sum_{i=1}^{\left\lfloor\frac{n}{r}\right\rfloor}(-1)^{i+1}\frac{1}{r^i*i!}\right)\\[1em]
    &= n!\sum_{i=0}^{\left\lfloor\frac{n}{r}\right\rfloor}(-1)^{i}\frac{1}{r^i*i!}
\end{align*}

I think you would agree with me that it looks disgusting. Generally because it seems to be filled with so many \left\lfloor <...> \right\rfloor strings.

Is it possible to define a command that writes \left and \right for me in the general case? I've read this post here Is it possible to write `\left(` `\right)` in one command? but it only covers the case when I want to use round brackets. What if I want to use square brackets, vertical bars or ceiling braces?

Kookie
  • 366
  • 2
  • 11

3 Answers3

8

You could load the mathtools package and use its \DeclarePairedDelimiter macro to create a macro called, say, \floor as follows:

\DeclarePairedDelimiter\floor\lfloor\rfloor

and replace all instances of \left\lfloor\frac{n}{r}\right\rfloor with \floor{\frac{n}{r}}. (For more information on the uses of \DeclarePairedDelimiter, please see section 3.6., "Paired delimiters", in the user guide of the mathtools package.)

And, since there are quite a few instances of \floor{\frac{n}{r}}, it's useful to create a shorthand macro for them, say,

\newcommand\flnr{\floor{\frac{n}{r}}}

In addition, I would replace all instances of the multiplicative * with \,, i.e., thinspace. Also, use \biggl( and \biggr) for the large parentheses in rows 5 and 6, as the parentheses produced by \left( and \right) are too large from a purely typographic/aesthetic perspective.

enter image description here

\documentclass{article}
\usepackage{mathtools} % for '\DeclarePairedDelimiter' macro
\DeclarePairedDelimiter\floor\lfloor\rfloor
\newcommand\flnr{\floor{\frac{n}{r}}} % handy shortcut macro

\begin{document}
\begin{align*}
n!-k 
    &= n! - \sum_{i=1}^{\flnr} (-1)^{i+1} \binom{n}{r,\dots,n-ir} ((r-1)!)^i(n-ir)!\,\frac{1}{i!}\\[1ex]
    &= n! - \sum_{i=1}^{\flnr} (-1)^{i+1} \frac{n!}{(r!)^i(n-ir)!}((r-1)!)^i(n-ir)!\,\frac{1}{i!}\\[1ex]
    &= n! - \sum_{i=1}^{\flnr} (-1)^{i+1} \frac{n!}{r^i(n-ir)!}(n-ir)!\,\frac{1}{i!}\\[1ex]
    &= n! - \sum_{i=1}^{\flnr} (-1)^{i+1}\frac{n!}{r^i\,i!}\\[1ex]
    &= n!\biggl(1-\sum_{i=1}^{\flnr} (-1)^{i+1}\frac{1}{r^i\,i!}\biggr)\\[1ex]
    &= n!\biggl((-1)^0\frac{1}{r^0\,0!}-\sum_{i=1}^{\flnr} (-1)^{i+1}\frac{1}{r^i\,i!}\biggr)\\[1ex]
    &= n!\sum_{i=0}^{\flnr} (-1)^{i}\frac{1}{r^i\,i!}
\end{align*}
\end{document}
Mico
  • 506,678
  • Quick q: why do you use \biggl and \biggr instead of left and right? Also, I noticed that the floor braces don't span to the bottom of the fraction. This can be a problem because if the fraction was bigger, then the floor braces would not fit. I think this is on me because I provided a poor example of code. – Kookie Jun 01 '20 at 09:21
  • 1
    I have essentially the same code, but with \floor{n/r}, which is much less intrusive. I'd also recommend !\, when the factorial is followed by something that doesn't add space by itself. – egreg Jun 01 '20 at 09:23
  • @Kookie - Because the parentheses produced if \left( and \right) are too big relative to what's optimal from a typographic point of view. For more on this topic see, e.g., this answer to the query Is it ever bad to use \left and \right?. [Shameless self-citation alert!] – Mico Jun 01 '20 at 09:24
  • Oh, that's interesting, [also, I really don't mind self-citation. A source is a source lul.] – Kookie Jun 01 '20 at 09:26
  • @egreg - I agree whole-heartedly on using inline-fraction notation instead of \frac. For the answer shown above, this may be achieved by defining \flnr not as \newcommand\flnr{\floor{\frac{n}{r}}} but as \newcommand\flnr{\mathclap{\floor{n/r}}}. On adjusting the spacing if ! (the factorial symbol) is followed by <something> (other than ), right?): Note the use of \, in the first three rows to provide a visual breather between (n-ir)! and \frac{1}{i!}. I failed to mention this change explicitly in my write-up, though. :-( – Mico Jun 01 '20 at 09:37
  • Oh, right. Thanks for telling me. – Kookie Jun 01 '20 at 09:43
6

My proposal is almost the same as Mico's, but with some significant differences:

  1. use n/r instead of \frac{n}{r};
  2. add \, when a factorial is followed by another object to be multiplied with (if that object doesn't produce space by itself, like in the last line);
  3. two instances of nested parentheses are dealt with using \bigl and \bigr;
  4. no additional vertical space is necessary (due to the n/r in the upper bound of summations).

I endorse the proposal of avoiding * for multiplication and substituting it with \, in those denominators; it's not generally necessary, these cases seem to want it, mostly because of the same letter in the exponent and in the following symbol.

\documentclass{article}
\usepackage{amsmath,mathtools}

\DeclarePairedDelimiter{\floor}{\lfloor}{\rfloor}

\begin{document}

\begin{align*}
n! - k 
&= n! - \sum_{i=1}^{\floor{n/r}}(-1)^{i+1}\binom{n}{r,\dots,n-ir} 
          \bigl((r-1)!\bigr)^i(n-ir)!\,\frac{1}{i!}
\\
&= n! - \sum_{i=1}^{\floor{n/r}}(-1)^{i+1} 
          \frac{n!}{(r!)^i(n-ir)!}\bigl((r-1)!\bigr)^i(n-ir)!\,\frac{1}{i!}
\\
&= n! - \sum_{i=1}^{\floor{n/r}}(-1)^{i+1} \frac{n!}{r^i(n-ir)!}(n-ir)!\,\frac{1}{i!}
\\
&= n! - \sum_{i=1}^{\floor{n/r}}(-1)^{i+1}\frac{n!}{r^i\,i!}
\\
&= n!\,\biggl(1-\sum_{i=1}^{\floor{n/r}}(-1)^{i+1}\frac{1}{r^i\,i!}\biggr)
\\
&= n!\,\biggl((-1)^0\frac{1}{r^0\,0!}-
            \sum_{i=1}^{\floor{n/r}}(-1)^{i+1}\frac{1}{r^i\,i!}\biggr)
\\
&= n!\sum_{i=0}^{\floor{n/r}}(-1)^{i}\frac{1}{r^i\,i!}
\end{align*}

\end{document}

enter image description here

egreg
  • 1,121,712
  • Wow! Looks great, thanks for the advice! Maybe this is just a style thing, but I'm not a big fan of inline maths. – Kookie Jun 01 '20 at 09:41
  • 1
    @Kookie Two-story fractions are really big; they're necessary for clarity in the main parts, but when superscripted they're generally better treated with the slashed form, provided numerator and denominator are simple. – egreg Jun 01 '20 at 09:57
  • Ok, I will consider using inline math a little more. – Kookie Jun 01 '20 at 10:59
2

You can use \qty from physics and \binom from amsmath, here's how they work:

\documentclass{article}
\usepackage{physics, amsmath}

\begin{document}

\begin{align*}
    S &= \qty(\sum_{k=0}^n \binom{n}{k} x^k y^{n-k})\\
    S &= \qty{\sum_{k=0}^n \binom{n}{k} x^k y^{n-k}}\\
    S &= \qty[\sum_{k=0}^n \binom{n}{k} x^k y^{n-k}]
\end{align*}

\end{document}

enter image description here

The physics package also helps with writing down matrices a little bit more easily with \mqty. You just need to write \mqty, then use the delimiters you want (), [], or {}, then, write whatever you like. Separate each column with & and each row with \\ just like in a usual array.

  • Do you know where I can find documentation for this? – Kookie Jun 02 '20 at 01:55
  • Yes, here's the documentation for the physics package: link with \qty at page 2 and \mqty at page 7

    Here's amsmath's user guide: link with \binom being at page 16

    The physics package is one of my favorite, it simplifies so many things and adds really useful commands for physics majors/physicists

    – Cat Admirer Jun 05 '20 at 15:03
  • Wau! The physics package is awesome! Although I am not studying physics, it contains commands to bold symbols and shortens things like derivatives. My mind is blown really. Thanks for showing me both guides! – Kookie Jun 05 '20 at 17:07