TeXForm of Grid, Add Seperation Lines and Color

7

2

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 ?

james

Posted 2018-04-03T06:41:57.097

Reputation: 2 783

Answers

4

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}

Nasser

Posted 2018-04-03T06:41:57.097

Reputation: 92 661

Very nice ! Thanks a lot. One more question: How can I make sure that the grid entries are all aligned along the left margin? – james – 2018-04-03T12:29:01.090

replace {|c|c|c|c|c|} with {|l|l|l|l|l|}. c means centerand l means left and r means right alignment in latex – OkkesDulgerci – 2018-04-03T13:17:13.820