0

How to create a TeXstudio macro to apply "Align Columns" for all matrices I want

After learning about about the command Align Columns I immediately fell in love with it and created a shortcut (Ctrl + Alt + T == Alt Gr + T) to use it in a blink of an eye. So far so good.

Recently I learned I could execute commands with TeXstudio's macros and not just use it to insert predefined text.

At this point I asked if it was possible to automatize the whole Search and Align Columns manual procedure.

I found the following infos about it:

Unfortunately, I could not install latexindent so I must find a way around, and I don't know the syntax enough to create from scratch.

I searched for info and tried some but I have no idea how to perform something like Align Columns of any shortcut in the MACRO environment.

The idea is have something like this

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{equation}
\begin{split}
  \begin{pmatrix}
    a             &              b \\
    c             &           d
  \end{pmatrix}  \\
  \begin{bmatrix}
    a             &        b \\
    c             &                 d
  \end{bmatrix}  \\
  \begin{Bmatrix}
    a             &                 b\\
    c             &            d
  \end{Bmatrix}   \\
  \begin{vmatrix}
    a             &       b \\
    c             &              d
  \end{vmatrix}  \\
  \begin{Vmatrix}
    a             &                  b \\
    c             &                  d
  \end{Vmatrix}  \\
\end{split}
\end{equation}

\end{document}

Apply the macro, which sketch I let below

%SCRIPT
options = "g"
scope = editor.document().cursor(0, 0, -1);
editor.find("matrix", options, scope, "");
//eval("AlignColumns");
//eval("Ctrl + Alt + T");
//editor.findNext();

enter image description here

and get the same result as manually go around using Search (F3) and Align Columns (Alt Gr + T) the whole document.

Adding array, tabular, split, and others environments that use & would also be something interesting.

enter image description here

3
  • 1
    Latexindent does this. If you find an alternative, let me know :) (author of latexindent)
    – cmhughes
    Commented Nov 25, 2020 at 18:35
  • @cmhughes, indeed, I tried to use latexindent, I saw your Youtube Video. latexindent does much more than just aligning tables and matrices, but I could not install Perl at the computer I use, therefore I asked for a specific workaround. Find a subset of word, apply the command. I don't know how to apply to command via macro.
    – FHZ
    Commented Nov 25, 2020 at 20:35
  • 1
    If you're on Windows, there is a Windows executable available. You just need it and default settings.yaml
    – cmhughes
    Commented Nov 25, 2020 at 21:11

1 Answer 1

1

Very simple macro to work around

It took a while but I found out TeXstudio command app.simulateKeyPress("<Keys>").

Combining the find I needed with the shortcut I'd already defined ("Ctrl+Alt+T"), I could emulate the same combinations I used manually into a single key combination due macro's shortcut.

%SCRIPT
editor.find("matrix", true, true, false, false);
app.simulateKeyPress("Ctrl+Alt+T");
//editor.findNext();

I must press the shortcut I chose (Shift+F1) twice, since I'm just looking for the word matrix.

I shall recognize this is not the most elegant solution, I just combined two tools. Probably someone with more skills than me can come with a better solution searching in a list of words or some other trick I don't know.

enter image description here

2
  • @cmhughes, this answer was the best I could perform with my current knowledge.
    – FHZ
    Commented Nov 25, 2020 at 23:26
  • Formatting code is tricky :)
    – cmhughes
    Commented Nov 26, 2020 at 12:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .