How can I add a math command with custom look and LaTeX code to LyX? I know this is possible for custom citation commands and character styles, so this should be possible for math commands as well!
EDIT: Actually, one can indeed change the alignment of array
in LyX by GUI means, Besides, I learnt that array
is a bad way of doing what I describe in the "Background" below and I should use alignat
. However, I think this question is relevant nevertheless, because there might be cases one wants to include own commands… for example an array
with >{\cmd}l
-style alignment specifyers, which is currently not possible to enter in the GUI.
Background:
I want to typeset a system of ordinary differential equations where I have similar terms in the same columns, left-aligned. I am not able to do that in LyX because all of the math environments I know center-align their contents.
The best I can up with using LyX-functions is to open a math equation (Ctrl–Shift–M) followed by typing \array
followed by the space key, which opens up a table-like environment where I can enter math:
However, I can not change the alignment of the columns like I can when creating a table outside the math environment. This results in code like the following:
\documentclass{article} % Added for MWE
\begin{document} % Added for MWE
\newcommand\d{\mathrm{d}} % Added for MWE
\[
\begin{array}{ccc}
\frac{\d V}{\d t} & =-\delta_{V} & -k_{inf}\cdot C\cdot V\\
\frac{\d C}{\d t} & =\lambda-\delta_{C} & -k_{inf}\cdot C\cdot V
\end{array}
\]
\end{document} % Added for MWE
Looking like this:
My problem is: I can not change the {ccc}
part of the array
environment yet.
I want to stay with LyX, because the preview feature saves me from repeatedly compiling my document, which takes quite some time.
The best I could come up with so far is to embed raw LaTeX code inside a "Preview" box (code same as above, except lll
):
So the best would be to define a command in LyX, which looks like array
in the LyX window, and invokes a custom environment. This uses the correct alignment which I define beforehand in a macro:
% Defined in LyX preamble
\newenvironment{myarray}{\begin{array}{\al}}{\end{array}}
% Defined as Evil Red Text (ERT) before Equation
\newcommand\al{llll}
% Created by LyX with the same appearance as if I had typed
% \array in LyX math mode
\[
\begin{myarray}
\frac{\d V}{\d t} & =-\delta_{V} & -k_{inf}\cdot C\cdot V\\
\frac{\d C}{\d t} & =\lambda-\delta_{C} & -k_{inf}\cdot C\cdot V
\end{myarray}
\]
Best Answer
Sure you can change the column specifiers of an
array
. After Ctrl + Shift + M, click the button on the math toolbar. That gives you a dialog looking something like this:Here you can set the number of rows and columns, and define the column specifiers in the text field at the bottom.
To change an existing column, place the cursor in a column and the go to Edit --> Rows and columns. The first three entries in that submenu is for left, center and right alignment. They have keyboard shortcuts as well:
If you really want to
You can get a better result with
array
as well, though I have to admit it's kind of a pain to do it in LyX using the GUI. Unless I've missed something of course, it's not like I ever use LyX (except for answering questions here).For this I would rather write the whole thing in an ERT. Or use
alignat
as Bernard mentioned, which is directly available via Insert --> Math --> AMS alignat.But anyways, if you like pain, start by going to Document --> Settings --> LaTeX preamble, and add
The
array
package allows you to use e.g.>{stuff}
to insertstuff
at the start of every cell in a column. The\diff
macro is for typesetting a differential d, remove the\mathrm
if you don't want it upright. Or don't use it at all.In your document, add a displayed equation and open the array dialog, as mentioned at the very top of this answer. In the field for specifying columns, write
This removes the space between columns, and adds an empty group at the start of the second and third column. The point of the empty group is to get correct space around the equal and minus signs. Don't add any spaces in this string though, that seemed to make it revert to
c
columns.Then fill in the array as you have done in your code, the fractions in the first cell, the
=
and\delta
term in the second, and thek_{\mathrm{inf}}
term in the third.Some notes:
I used a displayed fraction instead of the standard fraction:
To use the
\diff
macro, just type\diff
.There is a Fonts button on the math toolbar just to the right of the fraction button (image above), that you can use to get
\mathrm{inf}
in the subscripts for thek
. It's the one with four times the letter A.You need some vertical stretching of the
array
, so place the cursor just before thearray
, but inside the math environment, and write2.5
may not be ideal, so adjust to your liking.The result of all of this:
And here is the complete
.lyx
file that produces the above screenshot.