I thought M-x align-regexp & 1 1 y
on the region containing &
's would align all columns, but it does not, so I must be using it wrong. What is the correct way? Reminder: man says white spaces taken care of in interactive mode.
Baseline:
\begin{tabular}{clccc}
\toprule %
$D$ & & $P_u$ & $\sigma_N $ \\
(in) & & (lbs) & (psi) \\\otoprule %
5 & test 1 & 285 & 38.00 \\
& test 2 & 287 & 38.27 \\
& test 3 & 230 & 30.67 \\\midrule
10 & test 1 & 430 & 28.67 \\
& test 2 & 433 & 28.87 \\
& test 3 & 431 & 28.73 \\\bottomrule
\end{tabular}
M-x align-regexp &
:
\begin{tabular}{clccc}
\toprule %
$D$ & & $P_u$ & $\sigma_N $ \\
(in) & & (lbs) & (psi) \\\otoprule %
5 & test 1 & 285 & 38.00 \\
& test 2 & 287 & 38.27 \\
& test 3 & 230 & 30.67 \\\midrule
10 & test 1 & 430 & 28.67 \\
& test 2 & 433 & 28.87 \\
& test 3 & 431 & 28.73 \\\bottomrule
\end{tabular}
\end{table}
C-h a align-regexp
:
align-regexp is an autoloaded interactive byte-compiled Lisp function in ‘align.el’.
(align-regexp BEG END REGEXP &optional GROUP SPACING REPEAT)
Align the current region using an ad-hoc rule read from the minibuffer. BEG and END mark the limits of the region. Interactively, this function prompts for the regular expression REGEXP to align with.
For example, let’s say you had a list of phone numbers, and wanted to align them so that the opening parentheses would line up:
Fred (123) 456-7890 Alice (123) 456-7890 Mary-Anne (123) 456-7890 Joe (123) 456-7890
There is no predefined rule to handle this, but interactively, all you would have to do is to mark the region, call ‘align-regexp’ and enter "(".
REGEXP must contain at least one parenthesized subexpression, typically whitespace of the form "\(\s-*\)", but in interactive use, this is automatically added to the start of your regular expression after you enter it. Interactively, you only need to supply the characters to be lined up, and any preceding whitespace is replaced.
Non-interactively (or if you specify a prefix argument), you must enter the full regular expression, including the subexpression. Interactively, the function also then prompts for which subexpression parenthesis GROUP (default 1) within REGEXP to modify, the amount of SPACING (default ‘align-default-spacing’) to use, and whether or not to REPEAT the rule throughout the line.
See ‘align-rules-list’ for more information about these options.
The non-interactive form of the previous example would look something like: (align-regexp (point-min) (point-max) "\(\s-*\)(")
This function is a nothing more than a small wrapper that helps you construct a rule to pass to ‘align-region’, which does the real work.
Probably introduced at or before Emacs version 26.1.
UPDATE:
Based on the answer I did/got
C-u M-x align-regexp
"complex align using regexp:"
\(\s-*\)&
"Parenthesis group to modify (negative to justify):"
1
"Amount of spacing (column if negative):"
1
"Repeat throughout line:" y
\begin{tabular}{clccc} \toprule % $D$ & & $P_u$ & $\sigma_N $ \\ (in) & & (lbs) & (psi) \\\otoprule % 5 & test 1 & 285 & 38.00 \\ & test 2 & 287 & 38.27 \\ & test 3 & 230 & 30.67 \\\midrule 10 & test 1 & 430 & 28.67 \\ & test 2 & 433 & 28.87 \\ & test 3 & 431 & 28.73 \\\bottomrule \end{tabular}