5

I'm using csvreader from csvsimple package to read csv data and create table from it. My goal is to bold or somehow highlight first row and first column. Here is my MWE:

\documentclass{article}
\usepackage{setspace} % REQUIRED BY \singlespacing
\usepackage{csvsimple}
\usepackage{adjustbox}
\usepackage[bookmarks]{hyperref}

% \usepackage{filecontents}
% \usepackage{datatool}
% \usepackage{booktabs}

% Make csv in question
\begin{filecontents*}{matrix.csv}
;a01;a02;a03;a04;a05;b01;b02;b03;b04;b05;b06
a01;0;147632;181326;199650;271006;197441;215211;291227;131358;126435;268400
a02;147632;0;456;145466;259520;136489;165306;768;174075;160202;256612
a03;271006;259520;0;249326;0;249935;249608;241720;326917;292472;263386
a04;7;119114;0;0;246267;133549;263651;258427;6;193472;251168
a05;199650;145466;256502;0;0;135343;131358;259080;234963;250919;343499
b01;197441;6;133549;763;249935;0;162260;259712;233051;678;251516
b02;215211;165306;148099;6;249608;162260;0;256502;253780;228519;245948
b03;256612;270029;258427;6;241720;259712;256502;0;343499;308085;263651
b04;131358;8;215073;54;326917;233051;253780;0;0;678;313316
b05;126435;160202;193472;211280;292472;209561;228519;0;0;0;283208
b06;268400;256612;251168;6;263386;251516;245948;263651;256502;283208;0

\end{filecontents*}

\begin{document}

\begin{table}
  \centering
  \singlespacing
  \begin{adjustbox}{max width=\textwidth}
    \begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|}
      \hline
      \csvreader[separator = semicolon,
                 column count=12,
                 no head,
                 table head=\hline,
                 late after line=\\\hline,
      ]
      {./matrix.csv}{
          1=\one, 2=\two, 3=\three, 4=\four,
          5=\five, 6=\six, 7=\seven, 8=\eight,
          9=\nine, 10=\ten, 11=\eleven, 12=\twelve
      }
      {\one & \two & \three & \four & \five & \six & \seven & \eight & \nine & \ten & \eleven & \twelve}
    \end{tabular}
  \end{adjustbox}
  \caption{TestTableA}
  \label{tab:TestTableA}
  \doublespacing
\end{table}

Results are contained in ~\ref{tab:TestTableA} table

\end{document}

I've tried also replace table head=\hline, with table head = \hline \textbf{ rc } & \textbf{ a01 } & \textbf{ a02 } & \textbf{ a03 } & \textbf{ a04 } & \textbf{ a05 } & \textbf{ b01 } & \textbf{ b02 } & \textbf{ b03 } & \textbf{ b04 } & \textbf{ b05 } & \textbf{ b06 } \\\hline, based on suggestions here but it gives me an error (another drawback is that the code looks ugly).

PS: I'm not strictly limited to csvsimple seems that there are several interesting packages that might do the job.

PPS: Is it possible to get rid somehow of those 1=\one, 2=\two, 3=\three ... and \one & \two & \three .... and simply read the table as it is without specifying the columns?

4

This is using pgfplotstable and a slightly modified version of the code in PGFPlotsTable formatting problems.

enter image description here

\documentclass{article}
\usepackage{filecontents}
\usepackage{adjustbox}


% Make csv in question
\begin{filecontents*}{matrix.csv}
;a01;a02;a03;a04;a05;b01;b02;b03;b04;b05;b06
a01;0;147632;181326;199650;271006;197441;215211;291227;131358;126435;268400
a02;147632;0;456;145466;259520;136489;165306;768;174075;160202;256612
a03;271006;259520;0;249326;0;249935;249608;241720;326917;292472;263386
a04;7;119114;0;0;246267;133549;263651;258427;6;193472;251168
a05;199650;145466;256502;0;0;135343;131358;259080;234963;250919;343499
b01;197441;6;133549;763;249935;0;162260;259712;233051;678;251516
b02;215211;165306;148099;6;249608;162260;0;256502;253780;228519;245948
b03;256612;270029;258427;6;241720;259712;256502;0;343499;308085;263651
b04;131358;8;215073;54;326917;233051;253780;0;0;678;313316
b05;126435;160202;193472;211280;292472;209561;228519;0;0;0;283208
b06;268400;256612;251168;6;263386;251516;245948;263651;256502;283208;0
\end{filecontents*}


\usepackage{pgfplotstable}
\pgfplotstableread[col sep=semicolon]{matrix.csv}\WakanData
\begin{document}
\begin{table}
  \centering
  \begin{adjustbox}{max width=\textwidth}
   \pgfplotstabletypeset[
     string type,
     every first column/.style={
        postproc cell  content/.append style={
            @cell content/.add={\bfseries}{}
        },
       column name={},
       column type/.add={|}{} % remove this to remove first vertical line
     },
     assign column name/.style={%
        /pgfplots/table/column name={\textbf{#1}}%
    },
    every head row/.style={before row=\hline,after row=\hline}, % remove this and the next two lines to remove horizontal rules
    every odd row/.style={after row=\hline}, % horizontal rules
    every even row/.style={after row=\hline}, % horizontal rules
    column type/.add={}{|} % remove this to remove remaining vertical lines
    ]{\WakanData}
  \end{adjustbox}
  \caption{TestTableA}
  \label{tab:TestTableA}
\end{table}


Results are contained in ~\ref{tab:TestTableA} table

\end{document}
  • thank you, it is also possible contain tabulars somehow (\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|})? – Wakan Tanka May 14 '16 at 20:31
  • @WakanTanka Not sure what you mean. \pgfplotstabletypeset creates a tabular environment, you just don't see it. Do you just want add vertical lines between all the columns? Horizontal lines as well? – Torbjørn T. May 14 '16 at 20:36
  • exactly, I want to add horizontal and vertical lines. – Wakan Tanka May 14 '16 at 20:38
  • @WakanTanka Answer updated. – Torbjørn T. May 14 '16 at 20:41
  • Thank you, if anyone wants version without horizontal/vertical lines here it is pastebin.com/0VJVRud7 – Wakan Tanka May 14 '16 at 20:48
0

One simple way is make a data table in R and export to LaTeX with xtable (all within main document with knitr):

mwe

\documentclass{article}
\usepackage{booktabs}
\usepackage{amsmath}
\begin{document}
<<table,echo=F,results='asis'>>=
library(xtable)
table <- read.csv2("matrix.csv")
rownames(table) <- table[,1]
bold <- function(x) {paste('{\\textbf{',x,'}}', sep ='')}
print(xtable(table[,1:12]),     
      sanitize.rownames.function=bold, 
      sanitize.colnames.function=bold, 
      booktabs=T, size="tiny",
      )
@
\end{document}

Note: to make vertical lines, you can specify some option like align="|r|r|..." in xtable function and remove the booktabs option in print function. But seriously, do not do that!

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.