8

I am a beginner in Latex, using it tentatively for industrial documents. I have reached a point where the anticipated benefits from using it in a Corporate environment appear to me very significant. I am trying to write a paper explaining this to my company. I have made a quick comparison in the table of the MWE below. However, (in order to demonstrate a unique capability of LaTex) I would like to illustrate the point by a bar chart, possibly generated on the fly from the raw data I would embed in the Latex source. I have been trying to use \pgfplotstableread and \pgfplotstabletypeset to generate the table instead of the tabular option, but I could not succeed, probably due to the white spaces of the text.

  1. Could someone suggest a way to generate such a table outside of a tabular environment? Some reformatting of the text contents of the table is possible if there is no other way.
  2. Is there a way to generate a bar chart illustrating the point of file size without manually entering the coordinates?
  3. As a side request, could someone perhap suggest a presentation format that would provide the maximum evidence of the point illustrated?

    \documentclass[11pt]{article}
    \usepackage{graphicx}
    \usepackage{siunitx}
    \usepackage{pgfplotstable}
    \sisetup{table-figures-decimal = 3}
    \sisetup{table-number-alignment=center-decimal-marker}
    \begin{document}
    \centering
    \begin{table}
    \begin{tabular}{|l|S|S|}
    File type&{Word 2003}&{\LaTeX} \\
    \hline
    Native file size (Mo)& 1.07&.112\\
    Compressed native file size (Mo)&.268 & 0.022\\
    Final PDF file size (Mo) &.302& .272\\
    \end{tabular}
    \caption{Comparison of file size Word 2003 vs \LaTeX}
    \end{table}
    \end{document} 
    
David Carlisle
  • 757,742
Yves
  • 2,825
  • Please have a look at this answer which is a pretty nice demonstration and creates the data file on the fly with filecontents package. And for the presentation you can always use beamer package. – percusse Mar 29 '12 at 20:37

1 Answers1

9

You can generate a table and chart from basically any comma (or otherwise) delimited datafile. Here's an example for how your datapoints could be presented. I've chosen to transpose your datafile for the table, based on the idea that the numbers you want your audience to compare should be underneath each other, not next to each other. The transposition is done using \pgpflotstabletranspose.

I've made this example a bit fancy to show what can be done using \pgfplotstable. In a production environment, the code could be cleaned up a lot to use styles.

The datafile in this example is created using filecontents. In a real application, the CSV file would of course just live on the disk and be read by the TeX file.

In terms of the most effective presentation for this particular point, I think a simple column chart would be the most sensible choice, since it allows for a simple comparison of the pairs of data.

\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{siunitx}
\usepackage{pgfplotstable}
\sisetup{
    table-figures-decimal = 3,
    table-number-alignment=center-decimal-marker
}
\usepackage{filecontents}
\usepackage{booktabs}

\begin{filecontents}{data.csv}
{File type},{Word 2003},{LaTeX}
Native,1.07,.112
Compressed native,.268,.022
Final PDF,.302,.272
\end{filecontents}

\pgfplotstableread[col sep=comma]{data.csv}{\datatable}
\pgfplotstabletranspose[colnames from=File type, input colnames to=File Type]{\transposed}{\datatable}

\begin{document}
\begin{table}
\centering
\pgfplotstabletypeset[
    string type,
    every head row/.style={
        before row={\toprule & \multicolumn{3}{c}{File size (Mo)}\\},
        after row=\midrule
    },
    every last row/.style={after row=\bottomrule},
    multicolumn names,
    display columns/0/.style={
        multicolumn names=l,
        column name={},
        column type=l
    },
    display columns/1/.style={column type=S},
    display columns/2/.style={column type=S},
    display columns/3/.style={column type=S},
    every row 0 column 0/.style={
        postproc cell content/.style={
            @cell content=\textcolor{cyan!50!black}{\textbf{##1}}
        }
    },
    every row 1 column 0/.style={
        postproc cell content/.style={
            @cell content=\textcolor{orange!75!black}{\textbf{##1}}
        }
    }
]{\transposed}
\caption{Comparison of file size Word 2003 vs \LaTeX}
\end{table}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
    ybar,
    xtick=data,
    xticklabels from table={\datatable}{[index]0},
    enlarge y limits=upper,
    ymin=0,
    ylabel=File size (Mo),
    legend entries={Word 2003, \LaTeX}
]
\addplot [fill=cyan!70, draw=cyan!50!black]  table [
    x expr=\coordindex,
    y index=1] {\datatable};
\addplot[fill=orange, draw=orange!50!black] table [
    x expr=\coordindex,
    y index=2] {\datatable};
\end{axis}
\end{tikzpicture}
\caption{Graphical comparison of file size Word 2003 vs \LaTeX}
\end{figure}
\end{document}
Jake
  • 232,450
  • Thanks a lot. This is beautiful, and is a neat practical demo of one of the points I am trying to convey, ie that the direct translation of raw data from either a data file or from data embedded into the latex source is a leaner process than using a spreadsheet, making a graph, then pasting the graph into a word processor...and repeating th eloop a few times for adjusments... This approach of course implies skills that require some efforts and time to learn, as my difficulties to find a solution on my own illustrate... – Yves Mar 30 '12 at 14:42
  • I seem to have an error message "missing number, treated as zero that I cannot figure out how to solve, with line:\pgfplotstabletranspose[colnames from=File type, input colnames to=File Type]{\transposed}{\datatable} – Yves Mar 30 '12 at 15:42
  • Does that error occur if you just copy and paste the code from my answer? In that case, you might be using an old version of PGFplots (you can check your version by using \listfiles in your preamble and then looking at the end of the .log file). You'll want version 1.5.1 of PGFplots. – Jake Mar 30 '12 at 15:50
  • Yes. Thanks. I have tried to investigate, but I am not competent for the job...I will check the version. This might be the problem, but I have tried in my office also, where I use a fairly recent installation, another platform, and I had the same difficulty. – Yves Mar 30 '12 at 16:07
  • I use version 1.4.1 of pgfplot on my home computer. You were right...I am going to try to update. This will be an excellent exercise... – Yves Mar 30 '12 at 16:09
  • I have used my wifes's computer, that is more recent, and your code compiled beautifully. Thanks a lot. I am going to run a similar comparison of file sizes using files with many pictures. The file used for this table and plot was primarily a text file. – Yves Mar 30 '12 at 16:29
  • @Yves: I'm glad it works! I think this is one of the things LaTeX and PGFplots are really good for, the automated generation of reports, graphs and tables. If you bump into problems, don't hesitate to ask, this is a really helpful and active community. – Jake Mar 30 '12 at 18:49
  • I know how efficient this site is. As a matter of fact I plan to show in my paper how quickly and how well various users of the site responded to a request for improving the code of a small diagram I had posted. – Yves Mar 31 '12 at 06:30
  • I have edited my data to show file size in Ko instead of Mo. The largest number appears now 1070 in the table, (possibly because I use the \sisetup{group-digits=false} in my preamble, but 1,070 on the y axis of the plot. How can I make the numbers to be shown consistently? Ideally without the group digit separator, as I target a european audience. – Yves Apr 01 '12 at 08:46
  • @Yves: You can switch the group separators off. Take a look at http://tex.stackexchange.com/questions/31276/number-format-in-pgfplots-axis – Jake Apr 01 '12 at 08:54
  • Thansk. It works. I will try to find a global way to specify this option, in order to make it applicable to all plots. For some reason, I had imagined that a global \sisetup setting would affect the plots also. – Yves Apr 01 '12 at 09:58
  • @Yves: You can use \pgfplotsset{/pgf/number format/.cd, use comma, 1000 sep={}} in your preamble to set the option globally. The reason why \sisetup doesn't impact the plots is that they don't use siunitx for typesetting the numbers, but the PGF macro \pgfmathprintnumber (which is described in the PGF manual in detail). – Jake Apr 01 '12 at 10:49