6
$\begingroup$

I have the following grid (from documentation):

data = Import["ExampleData/classification.tsv"]
g = Grid[data, Alignment -> Left, Spacings -> {2, 1}, Frame -> All, 
  ItemStyle -> "Text", 
  Background -> {{Gray, None}, {LightGray, None}}]

Which results in this: enter image description here

I would like to use such a grid in a LaTex Document. So I extract the LaTex code using TeXForm:

TeXForm[g]

It almost works, but the seperation lines and the color is missing:

$\begin{array}{lllll} \text{Rank} & \text{Fruit Fly} & \text{Human} & \text{Pea} & \text{E.coli} \\ \text{Domain} & \text{Eukaryota} & \text{Eukaryota} & \text{Eukaryota} & \text{Bacteria} \\ \text{Kingdom} & \text{Animalia} & \text{Animalia} & \text{Plantae} & \text{Monera} \\ \text{Phylum} & \text{Arthropoda} & \text{Chordata} & \text{Magnoliophyta} & \text{Proteobacteria} \\ \text{Class} & \text{Insecta} & \text{Mammalia} & \text{Magnoliopsida} & \text{Proteobacteria} \\ \text{Order} & \text{Diptera} & \text{Primates} & \text{Fabales} & \text{Enterobacteriales} \\ \text{Family} & \text{Drosophilidae} & \text{Hominidae} & \text{Fabaceae} & \text{Enterobacteriaceae} \\ \text{Genus} & \text{Drosophila} & \text{Homo} & \text{Pisum} & \text{Escherichia} \\ \text{Species} & \text{D.melanogaster} & \text{H.sapiens} & \text{P.sativum} & \text{E.coli} \\ \end{array}$

I was wondering how it would be possible to add the speration lines and color ?

$\endgroup$
4
$\begingroup$

Here it is

Mathematica graphics

data = Import["ExampleData/classification.tsv"];
{n, m} = Dimensions[data];
s = "\\begin{tabular}{|" <> Table["l|", {m}] <> "}\\hline\n";
Do[
  Do[
   If[row == 1,
    color = "\\cellcolor[gray]{0.8}"
    ,
    If[col == 1,
     color = "\\cellcolor[gray]{0.5}"
     ,
     color = ""

     ]
    ];
   s = s <> color <> data[[row, col]];
   If[col == m, s = s <> "\\\\\\hline\n", s = s <> "&"]
   ,
   {col, 1, m}
   ]
  ,
  {row, 1, n}
  ];
s = s <> "\\end{tabular}";

Copy the s generated above (using copy-as-text) to your Latex file.

Mathematica graphics

And compile as follows

\documentclass[11pt,notitlepage]{article}
\usepackage[letterpaper,bindingoffset=0.2in,%
            left=1.2in,right=1.2in,top=.8in,bottom=.8in,%
            footskip=.25in]{geometry}

\usepackage{colortbl}

\begin{document} 

\begin{tabular}{|l|l|l|l|l|}\hline
\cellcolor[gray]{0.8}Rank&\cellcolor[gray]{0.8}Fruit Fly&\cellcolor[gray]{0.8}Human&\cellcolor[gray]{0.8}Pea&\cellcolor[gray]{0.8}E.coli\\\hline
\cellcolor[gray]{0.5}Domain&Eukaryota&Eukaryota&Eukaryota&Bacteria\\\hline
\cellcolor[gray]{0.5}Kingdom&Animalia&Animalia&Plantae&Monera\\\hline
\cellcolor[gray]{0.5}Phylum&Arthropoda&Chordata&Magnoliophyta&Proteobacteria\\\hline
\cellcolor[gray]{0.5}Class&Insecta&Mammalia&Magnoliopsida&Proteobacteria\\\hline
\cellcolor[gray]{0.5}Order&Diptera&Primates&Fabales&Enterobacteriales\\\hline
\cellcolor[gray]{0.5}Family&Drosophilidae&Hominidae&Fabaceae&Enterobacteriaceae\\\hline
\cellcolor[gray]{0.5}Genus&Drosophila&Homo&Pisum&Escherichia\\\hline
\cellcolor[gray]{0.5}Species&D.melanogaster&H.sapiens&P.sativum&E.coli\\\hline
\end{tabular}

\end{document}
$\endgroup$
  • $\begingroup$ Very nice ! Thanks a lot. One more question: How can I make sure that the grid entries are all aligned along the left margin? $\endgroup$ – james Apr 3 '18 at 12:29
  • $\begingroup$ replace {|c|c|c|c|c|} with {|l|l|l|l|l|}. c means center and l means left and r means right alignment in latex $\endgroup$ – Okkes Dulgerci Apr 3 '18 at 13:17

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.