[Tex/LaTex] Add math commands with custom look to LyX, to change `array` environment alignment

arrayshorizontal alignmentlyxmath-mode

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 (CtrlShiftM) followed by typing \array followed by the space key, which opens up a table-like environment where I can enter math:

Empty array environment in LyX

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:

View of above code

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):

LyX view: ERT inside of Preview environment

Desired result, manually

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 enter image description here button on the math toolbar. That gives you a dialog looking something like this:

enter image description here

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:

    enter image description here

  • 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:

enter image description here

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
\begin_header
\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
\end_header

\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