2

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}
    

2 Answers 2

0

Note this particular paragraph from the documentation:

Interactively, if you specify a prefix argument, the function
will guide you through entering the full regular expression, and
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.

In particular, notice that it only asks for the user to interactively specify these arguments if the user gives a prefix argument. You need to do C-u M-x align-regexp, and then it asks the additional questions.

4
  • I've updated accordingly: it is aligned throughout on &, but there is a big space in column 1.
    – user19777
    Commented Apr 28, 2023 at 20:00
  • Do you have other tables in the same document that you are aligning? Remember, this doesn’t know where the tables start and end, so it lines them all up.
    – db48x
    Commented Apr 28, 2023 at 20:03
  • Even if I select a region (as I did)? PS: there are a few isolated & in the file that could be corrupting the result because I tried in scratch and it worked.
    – user19777
    Commented Apr 28, 2023 at 20:05
  • FWIW, I don't get the big space in column 1.
    – NickD
    Commented Apr 28, 2023 at 22:43
0

Since this is a LaTeX file, I assume you are using LaTeX mode (AUCTeX). So you can place the cursor anywhere in the tabular environment and use M-x align-current. Actually, given the starting code (ugly ;), I advise to select first the whole tabular environment and use C-c C-q C-r (which runs the command LaTeX-fill-region).

After these two steps, I end up with :

\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}
2
  • 1
    What does C-q-r mean? What do I have to type? What function is bound to it?
    – NickD
    Commented May 4, 2023 at 20:41
  • 1
    @NickD Sorry, I meant C-c C-q C-r, which runs the command LaTeX-fill-region. I'll fix my answer. Commented May 5, 2023 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.