Lets say I have the following LaTeX code
\subsection{Convex sets}
A set \( \mathcal{S} \)
in \( \mathbb{R}^n \)
is said to be convex if
\[ \mathbf{x}_1, \mathbf{x}_2 \in \mathcal{S} \implies \lambda \mathbf{x}_1 +
(1-\lambda) \mathbf{x}_2 \in \mathcal{S} \text{~~~for all~~~} 0 < \lambda < 1 \]
I find that even after formatting it like this with a new line after each inline math environment it gets pretty unreadable after a while. I find that for texts that are full of math, in general, it is really hard to produce readable code.
Is there some best practices how to write more readable math in LaTeX?
I there something like LyX but with the possibility to write pure LaTeX and have commands being processed in the editor. Like if I write \textbf{b}
then b would show as bold and the \textbf{}
part would disappear. The when I hover with mouse over or place cursor behind b \textbf{}
shows up again. Maybe this is possible with LyX?
Best Answer
Most of what I say is covered by other answers, but I will write here my thoughts.
First of all, if you write a lot in LaTeX format, I think you should leave LyX and go for any plain text editor. In the (near) future you will be faster (reading and writing code).
About writing readable code. Here are some tips to make it readable:
x
to be bold (mathbf
), you must wantx
to be a vector (\vec{x}
). You will decide later what a vector looks like, but at first, you don't have to worry about it.\set
command (and later you will worry about how sets are displayed), but, as David Carlisle pointed, you can define shorter commands.\R
(\mathbb{R}
) to define the real numbers. It is an entity so I define a command to call them. But I wouldn't define\Rn
for\mathbb{R}^n
, because then you aren't writing what you want, you are fastening your code input (which shouldn't be done that way).\quad
is not easy readable for you, but I think it will in the future.\[
,\]
but I don't use\(
and\)
because I don't read math really good between those delimiters. So I changed them to$
.This is how I would write your code:
After that, I think you should differentiate writing code and fastening your input. Most of people defines commands to write faster (like
\newcommand\vx{\vec x}
from Niel de Beaudrap). I don't agree with that. To write faster you should use an editor (or a third party app) which lets you create snippets (i.e. I would make a snippet with\vec{x}
which would be called when you typevx
).At last, to fasten your code-reading you must use a program which shows your code with colors (a color for math, a color for commands, a color for text,…). This is an example of my code (the theme and font I show here is not the usual, but I'm not in my main operating system, however I don't hate this setup):
Summary
These are my ideas:
\mathbf{x} \in \mathbb{R}^n
or\vx \in \Rn
. I would go for\vec{x} \in \R^n
.