# Add math commands with custom look to LyX, to change array environment alignment

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}$


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:

• left: Alt + MCL
• center: Alt + MCM
• right: Alt + MCR

# 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

\usepackage{array}
\newcommand\diff{\mathop{}\!\mathrm{d}} % courtesy of egreg


The array package allows you to use e.g. >{stuff} to insert stuff 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

l@{}>{{}}l@{}>{{}}l


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 the k_{\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 the k. It's the one with four times the letter A.

• You need some vertical stretching of the array, so place the cursor just before the array, but inside the math environment, and write

\renewcommand\arraystretch{2.5}


2.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.

#LyX 2.2 created this file. For more info see http://www.lyx.org/
\lyxformat 508
\begin_document
\save_transient_properties true
\origin unavailable
\textclass article
\begin_preamble
\usepackage{array}
\newcommand\diff{\mathop{}\!\mathrm{d}} % courtesy of egreg
\end_preamble
\use_default_options true
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman "default" "default"
\font_sans "default" "default"
\font_typewriter "default" "default"
\font_math "auto" "auto"
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100 100
\font_tt_scale 100 100
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry false
\use_package amsmath 1
\use_package amssymb 1
\use_package cancel 1
\use_package esint 1
\use_package mathdots 1
\use_package mathtools 1
\use_package mhchem 1
\use_package stackrel 1
\use_package stmaryrd 1
\use_package undertilde 1
\cite_engine basic
\cite_engine_type default
\biblio_style plain
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\justification true
\use_refstyle 1
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false

\begin_body

\begin_layout Standard
\begin_inset Formula
$\renewcommand\arraystretch{2.5}\begin{array}{l@{}>{{}}l@{}>{{}}l} \dfrac{\diff V}{\diff t} & =-\delta_{V} & -k_{\mathrm{inf}}\cdot C\cdot\\ \dfrac{\diff C}{\diff t} & =\lambda-\delta_{C} & -k_{\mathrm{inf}}\cdot C\cdot V \end{array}$

\end_inset

\end_layout

\end_body
\end_document

• How could miss this?? Thank you! – akraf Dec 20 '16 at 10:42
• Wow! Really much easier than \begin{array}{lll}. 😸 – egreg Dec 20 '16 at 10:54
• @egreg You are right that for the MWE a LaTeX-only solution is easier, if I understand you correctly. However, for my real-life document, I appreciate the clear view on math formulae which LyX provides. Even more so when defining styles for custom LaTeX commands. – akraf Dec 20 '16 at 11:04
• @akraf I added something to my answer. – Torbjørn T. Dec 20 '16 at 16:45

The spacing will not be good. I think what you need is the alignat* environment, with two columns (hence 3 &). Further, you can save a lot of typing using the esdiff package. Also, infshould be typed upright, not like the three variables i, n, f. There's an \inf command for that. I don't know how all this is obtained with LyX, but the code should be the following:

\documentclass{article} % Added for MWE
\usepackage{mathtools}
\usepackage{esdiff} %

\begin{document} % Added for MWE

\begin{alignat*}{2}
\diff{V}{t} & ={} &-\delta_{V} & -k_{\inf}\cdot C\cdot V \\
\diff{C}{t} & = {}& \lambda-\delta_{C}& -k_{\inf}\cdot C\cdot V
\end{alignat*}

\end{document}


• You are correct with the typography, I left it out for code conciseness. Also, in my case, i did not mean "infimum", but "infection" by inf ;-) I'll have a look at alignat – akraf Dec 20 '16 at 11:07
• Even if it is infection, you must not type it as math variables: interletter spacing if different from text mode. If for demantic reasons you don't want to type _{\inf}, it's better to type it_{\text{inf}}, in my opinion. – Bernard Dec 20 '16 at 11:23