Proofy's blog

By Proofy, 7 weeks ago, In English

Can you spot any difference between $$$max$$$ and $$$\text{max}$$$ or $$$dp$$$ and $$$\text{dp}$$$?

How about $$$\lfloor \frac{max(a, b, c)}{b} \rfloor$$$ and $$$\left \lfloor \frac{\max(a, b, c)}{b} \right \rfloor$$$? Or even $$$\displaystyle \left \lfloor \frac{\max(a, b, c)}{b} \right \rfloor$$$!

While appreciating the efforts and the careful elaborate writing of the authors, I have faced many issues with latex writing in editorials that have frustrated me while reading over the years, so I thought of writing this blog to settle many of such issues and making our equations in editorials the prettiest possible.

I'm going to use this editorial in problem F div 1 an example and refer to it in things that can be improved. Of course, that doesn't mean any offence to satyam343's beautiful and careful writing of the editorial nor his/her LaTeX writing/skills, he/she has done it beautifully.

Also, note that any code that is quoted in this blog like this will be assumed to be enclosed between two dollar signs to represent LaTeX writing.

Tip 1. No italic writings in LaTeX

If we look at hint 1 in the editorial, we can see the author wrote $$$frequency[0]$$$ by just writing frequency[0]. The way I would recommend writing something like this is to use the command \text{your text} which makes a huge difference.

If you write it as \text{frequency}[0], it will appear as $$$\text{frequency}[0]$$$. More pretty, right?

You can even notice the difference if we made it as a block equation of sums (with two dollar signs)

$$$ \text{frequency}[0] + \text{frequency}[1] + \dots + \text{frequency}[n - 1] $$$

instead of

$$$ {frequency}[0] + {frequency}[1] + \dots + {frequency}[n - 1] $$$

Another example is when editorials are explaining a DP solution, they write $$$dp(i, j, k)$$$ or $$$DP(i, j, k)$$$ or $$$dp[i][j][k]$$$. One example is the same referenced editorial writes $$$dp[l][suff\_sum]$$$ as dp[l][suff\\_sum]

The way I recommend writing this is \text{dp}[l][\text{suff}\\_\text{sum}] which renders as $$$\text{dp}[l][\text{suff}\_\text{sum}]$$$, a big difference! In a similar manner, you can also write $$$\text{dp}(i, j, k)$$$ or $$$\text{DP}(i, j, k)$$$. I prefer $$$\text{dp}[i][j][k]$$$ using square brackets and a non-italic "dp."

You can see this can change $$$d = suff\_sum + initial\_cur\_sum$$$ to $$$d = \text{suff}\_\text{sum} + \text{initial}\_\text{cur}\_\text{sum}$$$. If it's annoying to write \text every time, just copy it and polish the whole text with it once in the end.

Of course, the standard is that up to preference, I recommend not using \text for one letter variables, like $$$x, y, z, a, b, c, l$$$ (we can see they would look like this $$$\text{x}, \text{y}, \text{z}, \text{a}, \text{b}, \text{c}, \text{l}$$$). However, two letters or more, \text makes it more clear and readable.

One final note in this regard: some functions in LaTeX are special and only need \ and no text like $$$\max, \min$$$, and $$$\gcd$$$. I wrote this like \max, \min, \gcd and not using \text. You can see $$$\max(a, b, c)$$$ is way better than $$$max(a, b, c)$$$ same with $$$\gcd(a, b, c)$$$ and $$$gcd(a, b, c)$$$.

Tip 2. Enclosing brackets

This is to change

$$$dp[i][new\_suffix\_max] = \max(dp[i][new\_suffix\_max], \min\limits_{k = p-1}^{i} best[k] + cost)$$$

to

$$$\text{dp}[i][\text{new}\_\text{suffix}\_\text{max}] = \max\left(\text{dp}[i][\text{new}\_\text{suffix}\_\text{max}], \min\limits_{k = p-1}^{i} \text{best}[k] + \text{cost}\right).$$$

There were differences explained in the previous tip, but this tip focuses on the parenthesis.

The first was rendered using the code is

dp[i][new\_suffix\_max]  = \max(dp[i][new\_suffix\_max], \min\limits_{k = p-1}^{i} best[k] + cost)

and the second was using

\text{dp}[i][\text{new}\_\text{suffix}\_\text{max}] = \max\left(\text{dp}[i][\text{new}\_\text{suffix}\_\text{max}], \min\limits_{k = p-1}^{i} \text{best}[k] + \text{cost}\right)

The major change I want to highlight is \left( and right) to enclose the parenthesis. This is when your parenthesis include some fraction, summation, minimum/maximum with limits, integration, exponentation, ...etc. The parenthesis or brackets are enlarged to enclose your expression.

Let's have some examples for comparison:

  • $$$dp[x] = dp[\frac{max(x + 1, 1)}{2}] + 1$$$ vs $$$\text{dp}[x] = \text{dp}\left[\frac{\max(x + 1, 1)}{2}\right] + 1$$$.
LaTeX code
  • $$$\lfloor \frac{\max(0, 1 - d)}{2} \rfloor$$$ vs $$$\left\lfloor \frac{\max(0, 1 - d)}{2} \right\rfloor$$$
LaTeX code
  • Consider the difference between $$$\displaystyle gcd(x + \sum_{i = 1}^n a_i + y, z)$$$ and $$$\displaystyle \gcd\left(x + \sum_{i = 1}^n a_i + y, z\right)$$$. The parenthesis were enlarged to enclose the summation inside.
LaTeX code

Final random tips for LaTeX and editorials in general

  • Use block equations. When you feel like your equation is a bit too big, use two dollar signs instead of one to enclose your equation. That makes them block in one separate line instead of inline. It's OK that it makes the editorial bigger; readability beats size.
  • Separate your paragraphs with new lines whenever it becomes crowded. Worry less about that the editorial becoming too big than it being cramped and unclear. Seek clarity.
Example
  • Use \dots.
Example
  • Use \displaystyle
Example
  • Use \cdot to denote a product, not *.
Example
  • Use subscripts if needed.
Example
  • Please, in problems like this, don't put names inside dollar signs to highlight. Use bold text. Write " Abdullah and Kifah " using **Abdullah** and **Kifah**, don't write "$$$Abdullah$$$ and $$$Kifah$$$" using $$$Abdullah$$$ and $$$Kifah$$$. If for some reason you insist on using dollar signs, use \text and display it as $$$\text{Abdullah}$$$ and $$$\text{Kifah}$$$
  • Never leave a variable without dollar signs. Don't say "an array of n elements", say "an array of $$$n$$$ elements".

Finally, if anyone has any other tips that I can add, please don't hesitate to share with us in the comments.

  • Vote: I like it
  • +220
  • Vote: I do not like it

»
7 weeks ago, # |
  Vote: I like it +20 Vote: I do not like it

Bravo.

I think \times is also a good choice: $$$2\times 4$$$.

I think we shouldn't use program language in math formula: $$$a=a+1$$$ or $$$a\gets a+1$$$ a\gets a+1?

I think we should use a\pmod b=c $$$a\bmod b=c$$$ instead of a\mod b=c $$$a\mod b=c$$$, and use a\equiv b\pmod c $$$a\equiv b\pmod c$$$ instead of other strange ways.

GCD has it's \gcd $$$\gcd$$$, LCM doesn't. But LCM has \operatorname{lcm} $$$\operatorname{lcm}$$$ though looks no difference from \text{lcm} $$$\text{lcm}$$$.

  • »
    »
    7 weeks ago, # ^ |
    Rev. 3   Vote: I like it +9 Vote: I do not like it

    I don't like \times to be honest except in special scenarios, but that's probably a matter of personal preference (I prefer cdot, and it's more common in mathematical texts).

    For assignment commands, I'd recommend $$$a := a + 1$$$ (a := a + 1) instead of $$$a \gets a + 1$$$ (a \gets a + 1), but that's also personal preference. But now that I think about it, $$$:=$$$ is read as "is defined to be," so may be $$$\gets$$$ is more appropriate.

    • »
      »
      »
      7 weeks ago, # ^ |
        Vote: I like it +5 Vote: I do not like it

      Yes. Due to the habits of the academic community around me, there are some difference. := is also very commonly used, I forgot it, I'm sorry.

      But mathematically speaking, it is best not to use \cdot in the middle of numbers, \times signs are better (at least in hand writing). Poor me lost two point last exam because this. Anyway both \cdot and \times are better than just a *.

  • »
    »
    7 weeks ago, # ^ |
    Rev. 4   Vote: I like it +18 Vote: I do not like it

    A pedantic $$$TeX$$$ nician here...

    For math operators like lcm, I suggest using \mathop{\mathrm{lcm}}, because it gives a better spacing control than just \text{lcm}, and also it works as a big operator, like: $$$\displaystyle \mathop{\mathrm{lcm}}_{k=1}^{n} a_k$$$ (\gcd also works as a big operator, so my definition provides feature parity with \gcd). [UPD: Also look at the post about \operatorname below.]

    In the case of mod, using \pmod as a binary operator may not be a best choice, because \pmod is originally meant for your latter usage (parenthesized modulo). I think \mathbin{\mathrm{mod}} is more appropriate when it's used as a binary operator ($$$a \mathbin{\mathrm{mod}} b$$$), or maybe \mathbin{\%} looks even better: $$$a \mathbin{\%} b$$$. [UPD: Also look at the post mentioning \bmod below.]

    For $$$\times$$$, I would avoid using it except for the matrix dimension (like "an $$$n \times m$$$ matrix"), because avoiding $$$\times$$$ and $$$\div$$$ is mostly a convention in math typesetting (except for elementary school textbooks), but I admit this is largely a matter of taste.

    • »
      »
      »
      7 weeks ago, # ^ |
        Vote: I like it +11 Vote: I do not like it

      I agree with everything you said except for the way you wrote $$$\TeX$$$ :"D

      • »
        »
        »
        »
        7 weeks ago, # ^ |
          Vote: I like it +11 Vote: I do not like it

        Oops! I'm leaving it as is to publicly shame myself, so I wouldn't forget putting a backslash again...

    • »
      »
      »
      7 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      For the binary mod there is \bmod: $$$a \bmod b = c$$$.

      For the $$$\times$$$, I recall some journal math style guides actually only allow \times, because it's more difficult to miss. Here in statements though we stick to \cdot for consistency.

      Is \mathopin any way better than \operatorname?

      • »
        »
        »
        »
        6 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Thank you for pointing out about \bmod.

        I didn't know about \operatorname and just googled about it a little bit. Apparently \mathop is a primitive command from plain TeX, and \operatorname comes from AMSMath. Actually, there are two variants: \operatorname and \operatorname*. The latter is meant for an operator that also works as a big operator (like \lim or \max), and the former is for something that can't be big (like \sin or \log).

        Here are some samples:

        • \operatorname{func} x, \operatorname{func}(x), \operatorname{dp}[x]: $$$\operatorname{func} x, \operatorname{func}(x), \operatorname{dp}[x]$$$.
        • \operatorname*{lcm}_{k=1}^{n} a_n: $$$\displaystyle \operatorname*{lcm}_{k=1}^{n} a_n$$$.

        I think \operatorname and \operatorname* are better for having control over whether the operator can be big or not (also it automatically applies \mathrm for you).

»
7 weeks ago, # |
Rev. 2   Vote: I like it +22 Vote: I do not like it

An insignificant addition but I always write algorithmic complexity as \mathcal{O}(N) $$$\mathcal{O}(N)$$$ instead of just O(N) $$$O(N)$$$ purely for aesthetic reasons.

Note that to view the difference properly you might want to change your math renderer into "Common HTML" or "SVG" (you can do it by right-clicking a LaTeX formula):

  • »
    »
    7 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The big-O notation $$$O(n)$$$ is actually a part of so-called Bachmann-Landau notations, like $$$o(n)$$$, $$$\Omega(n)$$$ or $$$\Theta(n)$$$. I personally would like to avoid using a curly O for $$$O(n)$$$ for consistency with other notations, because other symbols are never be written in a calligraphic font.

»
7 weeks ago, # |
  Vote: I like it +8 Vote: I do not like it

for the equations enclosed with two dollar signs instead of one, remember to put the punctuation right before the last two dollar signs instead of after. dont do this:

$$$1434\neq 665$$$

. instead, do this:

$$$1434 \neq 665.$$$

this is so that the punctuation doesn't go on the next line.

alternatively just look at this https://web.evanchen.cc/latex-style-guide.html

»
7 weeks ago, # |
  Vote: I like it +11 Vote: I do not like it

Thanks for a great blog! I think you presented a pretty nice way to handle long variable names.

What do you think about mixing subscripts ($$$a_i$$$) with the square bracket operator ($$$a[i]$$$)? There's a way to stick to the former, with something like $$$\text{dp}_{i,j}$$$, but it gets messy very quickly. On the other hand, you have to use subscripts for sequences defined in the statement. They are pretty interchangeable anyway, but it still bothers me sometimes.

  • »
    »
    7 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I think it wouldn't hurt to mention the change of notation at the beginning of the editorial.

    I still prefer $$$\text{dp}[i][j]$$$ specially when the indices are large. So, for $$$\text{dp}_{i, j + k}$$$ it's ok to use subscripts, but for something like

    $$$\displaystyle\text{dp}\left[\frac{i + 1}{2}\right]\left[j + \frac{j - i}{k}\right]$$$

    square braces are preferred for sure.

»
7 weeks ago, # |
  Vote: I like it +8 Vote: I do not like it

Ah cool

»
7 weeks ago, # |
Rev. 2   Vote: I like it +11 Vote: I do not like it

Yes thank you!!

I have a comment though. There is no reason to replace lower dots (...) with middle dots (\dots). It may make sense, for example, in matrix placeholders:

$$$\begin{pmatrix} \frac11 & \frac12 & \dots & \frac1{n+1} \\ \frac12 & \frac13 & \dots & \frac1{n+2} \\ \vdots & \vdots & \ddots & \vdots \\ \frac1{n+1} & \frac1{n+2} & \dots & \frac1{2n} \end{pmatrix}$$$

But if you just want three dots with the proper spacing and stuff, you can use \ldots (because I find $$$1+2+\dots+n$$$ weirder than $$$1+2+\ldots+n$$$).

  • »
    »
    7 weeks ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    In the example in the blog, it seems that ... did mess up the spacing, so yes may be ldots would be the optimal recommendation.

    Thanks for the valuable insights and feedback!

  • »
    »
    7 weeks ago, # ^ |
    Rev. 3   Vote: I like it +30 Vote: I do not like it

    In conventional math typesetting, \ldots ($$$\ldots$$$) and \cdots ($$$\cdots$$$) are used differently in the following manner:

    • \ldots is used within an enumeration. a_1, a_2, \ldots, a_n: $$$a_1, a_2, \ldots, a_n$$$.
    • \cdots is used between operators. a_1 + a_2 + \cdots + a_n: $$$a_1 + a_2 + \cdots + a_n$$$.

    \dots is a variant that automatically detects these two usage patterns and becomes \ldots or \cdots accordingly. Here are \dots versions of the above examples:

    • a_1, a_2, \dots, a_n: $$$a_1, a_2, \dots, a_n$$$,
    • a_1 + a_2 + \dots + a_n: $$$a_1 + a_2 + \dots + a_n$$$.
    • »
      »
      »
      7 weeks ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      Oh cool, I didn't know this! Maybe I prefer \ldots because it's more common in russian literature (like \geqslant $$$\geqslant$$$ instead of \geq $$$\geq$$$ or \varnothing $$$\varnothing$$$ instead of \emptyset $$$\emptyset$$$) or it's just my taste, but I didn't know that \dots was smarter than \cdots

»
7 weeks ago, # |
  Vote: I like it +8 Vote: I do not like it

Some more tips:

  • Use \le and \ge for $$$\le$$$ and $$$\ge$$$ (or \leqslant, \geqslant if you prefer) instead of writing <=, which shows up as $$$<=$$$ and is really ugly.
  • Use \pmod for things like $$$7 \equiv 2 \pmod{5}$$$ (7 \equiv 2 \pmod{5}) and \bmod for things like $$$7 \bmod 5 = 2$$$ (7 \bmod 5 = 2). If you use the wrong one (or \mod), then the spacing will look weird and off.
»
7 weeks ago, # |
  Vote: I like it +8 Vote: I do not like it

orz