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?

-
related? –  Marc van Dongen Dec 11 '12 at 10:57
The link provided by @MarcvanDongen is good. I would put a blank line after the heading, not break up the inline math, but on the other hand use plenty of line breaks and indents in the displaymath, splitting logical places such as the relation \implies and before the condition \text{...}. –  Andrew Swann Dec 11 '12 at 11:16
The title of this question is an oxymoron. –  morbusg Dec 11 '12 at 13:09
This is what LyX does by default. Go to math mode (e.g. ctrl+m and then type \textbf{ and you will see what happens) –  Xu Wang Dec 11 '12 at 15:39

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).

• One of the main features I found in LaTeX is the ability you have to write what you want. But don't fail at deciding what you want! I mean, in your code you (shouldn't want) don't want x to be bold (mathbf), you must want x 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.
• Next, if you usually define some sets you can define \set command (and later you will worry about how sets are displayed), but, as David Carlisle pointed, you can define shorter commands.
• This is a must have in my documents: \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).
• May be \quad is not easy readable for you, but I think it will in the future.
• This example is no so long, so you can't see how indenting your code benefits the reading of your code, but it really does.
@DavidCarlisle -- \S is the section sign; not good to redefine (and you'd be told that as soon as you tried \newcommand\S{...}). –  barbara beeton Dec 11 '12 at 13:43
Basically +1, but instead of your \V I'd agree with Manuel's answer and use \vec instead, and \renewcommand{\vec}{\mathbf}. And since I'm lazy, it'd be \vec x instead of \V{x}, which is actually faster to type on my German keyboard ({,} require a key combination, while space is the most trivial key to hit) –  Tobias Kienzler Apr 4 '13 at 7:30

In addition to Davids answer, you can use unicode with Luatex to make the code more readable. I know that this is difficult to input, and therefore not suitable for everyone, but I wanted to show what is possible:

\documentclass{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{unicode-math}

\setmathfont{Asana Math}

\newcommand\set{\mathcal}
\renewcommand\vec{\mathbf}

\begin{document}

A set $$\set{S}$$ in $$ℝ^n$$ is said to be convex if
$\vec{x}_1, \vec{x}_2 ∈ \set{S} ⇒ λ \vec{x}_1 + (1-λ) \vec{x}_2 ∈ \set{S} \text{~~~for all~~~} 0 < λ < 1$
\end{document}


Edit: As pointed out, the boldface x is U+1D431 𝐱 and the calligraphic S is U+1D4aE 𝒮. You can then leave out the command definitions, and write:

A set $$𝒮$$ in $$ℝ^n$$ is said to be convex if
$𝐱_1, 𝐱_2 ∈ 𝒮 ⇒ λ 𝐱_1 + (1-λ) 𝐱_2 ∈ 𝒮 \text{~~~for all~~~} 0 < λ < 1$

-
Yes +1 you could complete the transformation by using U+1D431 (mathematical bold small x) 𝐱 and U+1D4aE 𝒮 –  David Carlisle Dec 11 '12 at 11:09
I'd argue that yours is a prime example of bad practice because you hardcoded the style. For example, what if you want to change your \mathcal{S} to a different typeface? Do a global find-and-replace? Fine, but what if that also changes just changes the one \mathcal{S} that shouldn't have changed? –  Marc van Dongen Dec 11 '12 at 11:25
@MarcvanDongen It is not really hardcoded any more than $a + b + c$ hardcoded a choice of computer modern italic. In both cases the actual fonts can be set up elsewhere. It isn't always the right choice but it as advantages and disadvantages like any choice. –  David Carlisle Dec 11 '12 at 11:41
@MarcvanDongen (Remark: I'm not a fan of the "unicode-for-everything" approach) You can make the unicode char "subscript-2" \active (it is \active anyways) and then define it as _{2}. –  yo' Dec 11 '12 at 11:54
@MarcvanDongen It all depends... Personally I'd rather my input was all ascii markup with the minimum of syntax highlighting. I prefer to see xml/html that way too. But I have learned that I'm in a minority, and the OP specifically asked to see less markup, and using a unicode based system is a reasonable approach to achieving that. It also helps if you are sharing expressions with non-Tex users eg Word more or implements the inline Unicode math described in unicode.org/notes/tn28 which is rather close to the end result of using unicode for everything in xetex/luatex. –  David Carlisle Dec 11 '12 at 12:52

I personally use emacs with AUCTeX and fold-mode. AUCTeX renders math, figures, and titles and shows them exactly as they will look like in the final document. All you need to do is the type C-c C-p C-d to generate all the images. When your caret moves to a generated images it is converted back to text so you can edit the LaTeX.

This is what it looks like when editing a generated image with AUCTeX:

For things that AUCTeX doesn't handle, like plain text \emph{}, \cite{}, enumerate (and friends), and \ref{} (and others) I use fold-mode. fold-mode can work alone if you do not want to use AUCTeX for full rendering. When used without AUCTeX It will handle titles and footnotes (previously handled by AUCTeX) and will use unicode characters for math symbols; you can see the letter \lambda and \in in the screenshot.

The "[c]" is a hidden citation, hidden by fold-mode, to see or edit it just move the caret to it. Same for "[f]" which is a hidden footnote by fold-mode, while with AUCTeX it is given its proper number "1" as displayed in the second image. The green italic "convex" is "\emph{convex}", rendered like this by fold-mode.

-

One interesting option could be using Unicode symbols, e.g. ∫ instead of \int and ℝ instead of \mathbb{R}, and use \DeclareUnicodeCharacter.

There's the neo keyboard layout (German site, sorry) which offers six layers (instead of the usual two switched via shift), one of which serves to input the most common mathematical symbols:

The disadvantage is that the layout also remaps your Latin letters (it's an attempt to improve German typing, similar to Dvorak). But you can see this as an inspiration to modify your QWERTY (or whatever layout you prefer) to include this additional layer. (Sidenote: You can create Windows keyboard layouts with MSKLC)

The neo-layout website also has a German wiki article on how to set this up with LaTeX or XeTeX, and they provide the uniinput package for the mentioned \DeclareUnicodeCharacter of probably all relevant unicode symbols (including e.g. greek letters).

update I figured out there actually is a Unicode block Mathematical Alphanumeric Symbols including bold letters so you could write 𝐱 instead of \mathbf{x}. Typing that could be implemented via a deadkey, e.g. AltGr+b followed by x. So your example could be input as

\subsection{Convex sets}
A set $$𝒮$$
in $$ℝⁿ$$
is said to be convex if
$𝐱₁, 𝐱₂ ∈ 𝒮 ⇒ λ 𝐱₁ + (1-λ) 𝐱₂ \in 𝒮 ∀ 0 < λ < 1$


which looks pretty readable to me. (I took the liberty of replacing your for all as well)

-
This will neither save you trouble after changing your mind to use \mathcal instead of \mathbb later on nor display \textbf conveniently though, you'll have to use e.g. LyX for that... –  Tobias Kienzler Dec 11 '12 at 17:21
Do you know if there is something like this ready to be used with US-international as the first layer? (for GNU/Linux, or Ubuntu in particular) –  alfC Dec 11 '12 at 20:40
@alfC Unfortunately not, maybe someone at unix.stackexchange.com does. But you can download their Linux files or browse the SVN repository to see how it's done and then adapt it yourself... (and then put a link here so others can benefit as well ;) –  Tobias Kienzler Dec 12 '12 at 7:24
I opened a question in askubuntu.com/q/228050/15943 –  alfC Dec 12 '12 at 7:59
I tried but the code seems quite cryptic svn.neo-layout.org/linux/xmodmap/neo_de.xmodmap –  alfC Dec 14 '12 at 5:37

I do a lot of my early draft writing in LaTeX, so I place a substantial value in being able to quickly read my own LaTeX code.

As a result, I adopt a style which is half-way between the "LaTeX way" (of using the correct delimiters for math environments and semantic names for macros) and quick-and-dirty macro usage (to save not only on keystrokes made, but on characters to re-read). The following is a typical way that I would render your code-segment.

% In the pre-amble
% ====================

% A semantic command for vectors
\renewcommand\vec[1]{\mathbf{#1}}

% A syntactic command for special symbols which I would
% consistently use for a given purpose.
\newcommand\R{\mathbb{R}}
\newcommand\cS{\mathcal{S}}

% In the document body
% ====================

\subsection{Convex sets}

A set $\cS$ in $\R^n$ is said to be \emph{convex} if
\begin{align}
\vec x_1, \vec x_2, \in \cS
&\implies
\lambda \vec x_1 + (1-\lambda) \vec x_2 \in \cS
&
\text{for all $0 < \lambda < 1$.}
\end{align}


I don't bother adding newlines in the middle of sentences, except for things such as footnotes and ends of lines (which is useful to do if you use a content revision system to track changes). But I add them liberally in my displayed math to better reveal the structure of what I'm writing, especially in tabulated environments such as align. You could also just use an equation environment instead, in which case I would write:

$$\vec x_1, \vec x_2, \in \cS \implies \lambda \vec x_1 + (1-\lambda) \vec x_2 \in \cS \quad \text{for all 0 < \lambda < 1.}$$


Some further remarks on my approach to defining the macros:

• If you only have a few vectors in your text, you can make your mathematics somewhat more terse by defining

\newcommand\vx{\vec x}


to speed up your (or your collaborators') personal lexing process; how this affects one's ability to parse the mathematics is another matter. As with \cS, this sort of naming convention depends on the reader (most probably just you, but also any collaborators) becoming accustomed to parsing the name of the macro as an adjective-noun couplet: a "calligraphic S", a "vector x", and so on.

• As for \R, this sort of naming convention ought only to be used for a single style thoughout all of your documents for symbols representing very important objects (such as blackboard-bold symbols denoting number-sets which you are likely to refer to in any given paper).

Use such shortcuts with discretion, both for your own sanity and those of your colleagues who might also work on the document with you.

-