# Creating a Macro for an n-by-m Matrix in LyX

Specifically, I'd like to create a matrix such as

$\left[\begin{array}{cc|c} a_{1} & a_{2} & a_{3}\\ b_{1} & b_{2} & b_{3}\\ c_{1} & c_{2} & c_{3} \end{array}\right]$


But in an arbitrary fashion in LyX. I'd like to do this in a way the utilizes the fact that LyX displays the 'boxes' where one can put in numbers, as opposed to typing it in beforehand. Although, I'd be fine in doing it without that feature. So far, I've only been able to create a very basic macro in my own .sty file defining a 2x2 matrix but I cannot get LyX to display the 'boxes' and can only get the macro to work if I add in curly braces and put in the numbers myself.

I should add that I'm decent at LyX and TeX, but no expert by any stretch of the imagination. My use is restricted to its capabilities as a note taking device, as my handwriting has always been illegible. However, I can follow some of the technical jargon, so don't be too afraid to throw something at little specific at me!

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – cslstr Jun 16 '14 at 22:15
• Can you explain more clearly what you want to do. From what you have written it is not clear to me what the macro is supposed to produce. Are a, b and c meant to be the parameters to your macro? If so \newcommand\MyMat[3]{\ensuremath{\left[\begin{array}{cc|c} {#1}_{1} & {#1}_{2} & {#1}_{3}\\ {#2}_{1} & {#2}_{2} & {#2}_{3}\\ {#3}_{1} & {#3}_{2} & {#3}_{3} \end{array}\right] is what you want. – user30471 Jun 17 '14 at 6:38

To explain it more detailled as in @Andrew's comment (there a some curly braces missing at the end).

I will explain it in two ways: the LyX and the LaTeX coding way.

## LyX macro input

Insert a new blank macro by clicking on menu "View > Math > Macro". A macro field with prefilled macro name "\newmacroname" is inserted:

Change "\newmacroname" to the name you like, here e.g. "\MyMat". Then place the cursor in the blue field on the right, labelled "LyX". Put in the math stuff you want. Insert placeholders (1st: \#1, 2nd: \#2, ...) for each parameter on the place where you want them. For your example it would like that*:

And that's it. You can use the macro right away in math mode. Just type \MyMat. Now if you type in the first placeholder, every other place with the same placeholder is automatically also filled.

• There seems to be a bug with inserting the vertical line in the matrix, so you must use decoration type "None" and horizontal alignment "cc|c".

## LaTeX newcommand input

Copy the this code as text in LyX:

\newcommand{\MyMat}[3]{
\left[
\begin{array}{cc|c}
#1_1 & #1_2 & #1_3\\
#2_1 & #2_2 & #2_3\\
#3_1 & #3_2 & #3_3
\end{array}
\right]
}


Highlight it and turn it to math mode (Ctrl + M). Then you can use it right away as a macro in math mode (like above). Or write in text mode \MyMat{a}{b}{c} and convert it also in math mode, then it will be already filled. That's pure LaTeX (-;

• You may not want to use \matrix, as that is already defined by LaTeX, so \newcommand\matrix{...} will throw an error. – Torbjørn T. Feb 24 '15 at 22:05
• @TorbjørnT. Thanks for your hint. I changed my answer that it will be also useful for LaTeX users. However, \matrix works in LyX. It uses \global\long\def\matrix{...}. – Dominik Feb 25 '15 at 15:51
• Yes, \def doesn't care whether the macro already exists, it just overwrites it silently. Could probably cause problems it you re-\def the wrong macro. – Torbjørn T. Feb 26 '15 at 8:02