# Converting multiple table cells to math mode Lyx

I am importing a .CSV file to LyX to make a table. This procedure works nicely, but numbers in the cells of the table are interpreted as normal text. I can select an individual cell press cmd+m and then that cell is converted to math mode.

However, when I select several cells and then hit cmd+m then the command is disabled and I can't change the cells' formatting.

Any advice on how to do this?

This solution is the least elegant, but it shows a manual approach that can be useful in similar situations:

What are the dimensions of the part of the table that you would like to convert to math mode? Suppose it is 3x5 so that we need to convert 15 cells to math mode.

Alt-X to open and activate the mini-buffer (this is at the very bottom of your screen). Enter the following command:

repeat 15 command-sequence char-backward-select; math-mode; cell-forward


Explanation:

repeat 15 repeats the following command 15 times.

command-sequence says the command is actually a sequence of commands separated by semi-colons.

char-backward-select select the whole cell.

math-mode converts the selection to math mode.

cell-forward moves the cursor to the beginning of the next cell.

• Nice. Only problem is if there is one column with text, that you don't want in math mode. Is it possible to do this column-wise instead? – Torbjørn T. Jun 6 '13 at 16:56
• @TorbjørnT. Good point. It gets even uglier in this case. You can replace cell-forward with down but then you have to deal with the last cell because currently if LyX selects the whole table (which is what char-backward-select will do on the last iteration) and receives a math-mode it replaces the table with an empty math-mode. (This is a bug I'll try to fix for 2.1.1). You can replace cell-forward with word-select and then if you put the correct number of rows in the repeat argument, it should work as expected. If you are off by one you could be in trouble again (table deleted). – scottkosty Jun 6 '13 at 17:34
• @TorbjørnT. and replacing with word-select has the problem that if there is more than one 'word' (in the LyX sense) in a cell, only the first word will be put into math-mode. This can be fixed of course, but I think it's already ugly enough :) – scottkosty Jun 6 '13 at 17:35

You can change entire columns to math-mode, or change the column type to an S column (from siunitx) in the table settings. The table settings is found by right clicking in a cell, and selecting More .. --> Settings.

Custom column specifiers is written in the LaTeX argument box, in the bottom of the dialog. To use an S column, simply write S in this box. To set the entire column in math-mode (and centered), write >{$}c<{$}, as in the screenshot below. If you want the number column to be right or left aligned, replace the c with r or l, respectively.

To load siunitx, go to Document --> Settings --> LaTeX preamble, and add

\usepackage{siunitx}


To use >{} <{} with column specifiers you have to load the array package, so add

\usepackage{array}


to the preamble.

To change a different column, just click in the one you want to change to place the cursor there, you don't have to close the table settings dialog. You'll see the Current cell in the bottom left of the dialog change.

If there are some cells in the column that you do not want to have in math mode, you can do that by using \multicolumn: right click the cell, choose More --> Multicolumn. The same applies for non-number cells in S columns.

For S-columns there is another way to 'escape' non-number cells as well, that is to add braces in ERTs before and after the cell content. That is, add an ERT containing { at the beginning of the cell, and one containing } at the end.

• Thanks for a thorough reply. I couldn't quite get it to work (probably my stupidity). When I enter >{$}c<{$} in the box, I get an error and the document doesn't compile: \begin{tabular}{>{$}c<{$}lllll} You're in trouble here. Try typing <return> to proceed. If that doesn't work, type X <return> to quit. – JoePhysicsNut Jun 6 '13 at 17:37
• @JoePhysicsNut Yes, I see, sorry. The array package is needed, so add \usepackage{array} to Document --> Settings --> LaTeX preamble. (I had loaded siunitx when I tried this, and that loads array, so I didn't get that error.) – Torbjørn T. Jun 6 '13 at 18:04

Have you considered using siunitx package? It allows you to create S-type columns, which are used precisely for this purpose.

The examples from the siunitx package documentation (section 4.6):

\documentclass{report}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{siunitx}
\usepackage{booktabs}

\begin{document}

\begin{table}
\caption{Standard behaviour of the \texttt{S} column type.}
\label{tab:S:standard}
\centering
\begin{tabular}{S}
\toprule
{Some Values} \\
\midrule
2.3456 \\
34.2345 \\
-6.7835 \\
90.473 \\
5642.5 \\
1.2e3 \\
e4 \\
\bottomrule
\end{tabular}
\end{table}

\begin{table}
\caption{Detection of surrounding material in an \texttt{S}
column.}
\label{tab:S:extras}
\centering
\begin{tabular}{S[color=orange]}
\toprule
{Some Values} \\
\midrule
12.34 \\
\color{purple} 975,31 \\
44.268 \textsuperscript{\emph{a}} \\
\bottomrule
\end{tabular}
\end{table}

\end{document}


The produced output looks like:

• thanks for the reply! If I have a table in Lyx that I got from importing the CSV file, how do I enter it into the siunitx structure? – JoePhysicsNut Jun 6 '13 at 15:35
• You just need to use the S type column for your columns with numbers :). siunitx takes care of everything and even centers the number according to the decimal point (a little extra beauty). Take a look at the examples I provided – Mario S. E. Jun 6 '13 at 16:39