1
$\begingroup$

I have an array in Mathematica, which I would like to export to a file in TeX format.

data={{1.2, 1.0429, "0.440773(65)", 1.665 \[PlusMinus] 1.85*10^-2}, 
{1.3, 2.9496, "0.859618(21)", 0.911 \[PlusMinus] 3.02*10^-2},
{1.4, 0.9916, "0.191505(74)", 4.755 \[PlusMinus] 4.64*10^-2}, 
{1.5, 1.6394, "0.811863(52)", 1.507 \[PlusMinus] 8.05*10^-2}};

Due to the options that I'm using to generate my document with LaTeX, the entries in the last column must be enclosed in {$ xxx $}, so the output must look like this:

  1.2 & 1.0429 & \text{0.440773(65)} & {$ 1.665\pm 0.0185 $} \\
  1.3 & 2.9496 & \text{0.859618(21)} & {$ 0.911\pm 0.0302 $} \\
  1.4 & 0.9916 & \text{0.191505(74)} & {$ 4.755\pm 0.0464 $} \\
  1.5 & 1.6394 & \text{0.811863(52)} & {$ 1.507\pm 0.0805 $} \\

Grid[data]//TeXForm almost yields the correct result but it doesn't enclose the entries of column four in {$ xxx $}. Is there a way to specify the format of the columns?

$\endgroup$
1
$\begingroup$

four in {$ xxx $}. Is there a way to specify the format of the columns?

I am a little confused. Why do you need to do this? In your Latex, just enclose the whole output in an equation so the whole thing becomes in math mode.

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\[
\begin{array}{cccc}
 1.2 & 1.0429 & \text{0.440773(65)} & 1.665\pm 0.0185 \\
 1.3 & 2.9496 & \text{0.859618(21)} & 0.911\pm 0.0302 \\
 1.4 & 0.9916 & \text{0.191505(74)} & 4.755\pm 0.0464 \\
 1.5 & 1.6394 & \text{0.811863(52)} & 1.507\pm 0.0805 \\
\end{array}
\]                            
\end{document}

You can add the \[...\] inside your latex document, or add it inside Mathematica, depending on how you are doing things. When you compile the above in Latex, here is the output

enter image description here

I'm using tabular

Well, you could always convert it by "hand" as this is easy enough. (this is how I actually generate all my Latex documents from Mathematica)

data = {{1.2, 1.0429, "0.440773(65)", 
    1.665 \[PlusMinus] 1.85*10^-2}, {1.3, 2.9496, "0.859618(21)", 
    0.911 \[PlusMinus] 3.02*10^-2}, {1.4, 0.9916, "0.191505(74)", 
    4.755 \[PlusMinus] 4.64*10^-2}, {1.5, 1.6394, "0.811863(52)", 
    1.507 \[PlusMinus] 8.05*10^-2}};
toX[s_] := ToString@TeXForm[s];
{nRow, nCol} = Dimensions[data];
s = "\\begin{tabular}" <> Table["c", {nCol}] <> "}
   ";
Do[
  Do[
   If[m == 1, s = s <> toX@data[[n, m]],
    If[m == nCol, s = s <> "&{$" <> toX@data[[n, m]] <> "$}\\\\
       ", s = s = s <> "&" <> toX@data[[n, m]]]]
   , {m, 1, nCol}
   ]
  , {n, 1, nRow}
  ];
s = s <> "\\end{tabular}
   ";

s

Gives

\begin{tabular}{cccc}
1.2&1.0429&\text{0.440773(65)}&{$1.665\pm 0.0185$}\\
1.3&2.9496&\text{0.859618(21)}&{$0.911\pm 0.0302$}\\
1.4&0.9916&\text{0.191505(74)}&{$4.755\pm 0.0464$}\\
1.5&1.6394&\text{0.811863(52)}&{$1.507\pm 0.0805$}\\
\end{tabular}

Compiles as

enter image description here

Do you want frame? Then do

data = {{1.2, 1.0429, "0.440773(65)", 
    1.665 \[PlusMinus] 1.85*10^-2}, {1.3, 2.9496, "0.859618(21)", 
    0.911 \[PlusMinus] 3.02*10^-2}, {1.4, 0.9916, "0.191505(74)", 
    4.755 \[PlusMinus] 4.64*10^-2}, {1.5, 1.6394, "0.811863(52)", 
    1.507 \[PlusMinus] 8.05*10^-2}};
toX[s_] := ToString@TeXForm[s];
{nRow, nCol} = Dimensions[data];
s = "\\begin{tabular}{" <> Table["|c", {nCol}] <> "|}\\hline
   ";
Do[
  Do[
   If[m == 1, s = s <> toX@data[[n, m]],
    If[m == nCol, s = s <> "&{$" <> toX@data[[n, m]] <> "$}\\\\\\hline
       ", s = s = s <> "&" <> toX@data[[n, m]]]]
   , {m, 1, nCol}
   ]
  , {n, 1, nRow}
  ];
s = s <> "\\end{tabular}
   ";

s

gives

\begin{tabular}{|c|c|c|c|}\hline
1.2&1.0429&\text{0.440773(65)}&{$1.665\pm 0.0185$}\\\hline
1.3&2.9496&\text{0.859618(21)}&{$0.911\pm 0.0302$}\\\hline
1.4&0.9916&\text{0.191505(74)}&{$4.755\pm 0.0464$}\\\hline
1.5&1.6394&\text{0.811863(52)}&{$1.507\pm 0.0805$}\\\hline
\end{tabular}

Which compiles as

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks. In LaTeX I'm using tabular along with siunitx in order to align the numbers by decimal points. If an entry has a symbol in math mode, it has to be wrapped in curly braces. Otherwise LaTeX won't compile :-(. $\endgroup$ – Granados Jul 7 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.