I am very happy with vim and the tabular
plugin. To align the table, place the cursor anywhere inside the (nonaligned) table and type :Tabularize /&
. The result is a nicely aligned table.
Before:
moon&456456456423423534&2342523
sun&234324&3453
Then a :Tabularize /&
, after:
moon & 456456456423423534 & 2342523
sun & 234324 & 3453
If you need this often, you can assign a custom shortcut for this.
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:
- 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.
- I use
\[
,\]
but I don't use \(
and \)
because I don't read math really good between those delimiters. So I changed them to $
.
- Personally, I don't write comments in my code unless I need to edit it later or something (I use comments as reminders). If you read code fast you don't need comments which explains what are you doing (in my case, they break my concentration).
This is how I would write your code:
\documentclass{article}
\usepackage{mathtools}
\usepackage{amsfonts}
\renewcommand\vec[1]{\mathbf{#1}}
\newcommand\set[1]{\mathcal{#1}}
\newcommand\R{\mathbb{R}}
\begin{document}
\subsection{Convex sets}
A set $\set{S}$ in $\R^n$ is said to be convex if
\[
\vec{x}_1, \vec{x}_2 \in \set{S} \implies \lambda \vec{x}_1 + (1-\lambda) \vec{x}_2 \in \set{S} \quad \text{for all } 0 < \lambda < 1
\]
\end{document}
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 type vx
).
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:
- Write what you want (but don't misunderstand what you really want). The commands you use have to be entities, not just 'shortcuts'. Later, you will care about how will it look like.
- Don't use commands as snippets (use a good editor or a third party app to make use of snippets).
- Indent your code.
- Use an editor which displays the code with different colors.
- Again, if you want your code to be readable don't use
\mathbf{x} \in \mathbb{R}^n
or \vx \in \Rn
. I would go for \vec{x} \in \R^n
.
Best Answer
I write my tables always in a pretty printed form to be able to recognize colums and rows in the LaTeX code.
My best practice is:
&
below each other so that I can see in the code the columns of my table. I keeo this structure even if I have to use the macros\multirow
or\multicolumn
.\\
) below each other.An example:
Writing your table in this way allows you to change rows or columns better. With a little practice you will see it is usable ...
I write all my tables in this way.