4

I'm trying to recreate a wholesale price list in LaTeX so that the data can be updated automatically from CSV files.

I'm able to import data using \csvautolongtable but I'm struggling with the formatting. This will be shown to customers as well as used internally so I would like it to look at least as good as the Excel tables the data originally came from (below):

enter image description here

My questions are as follows:

  1. Is there a way to recreate a table with similar (ideally better) formatting using csvsimple? Particularly the wrapping of long lines, prices in bold and center-aligned headers.

    Wrapping is especially important because otherwise I need to set the font to a tiny size or the table doesn't fit on the page. I'm using longtable because some other data fills more than one page.

  2. Prices are in GBP, how do I get csvsimple to read the £ symbols in the CSV files? It's currently ignoring them.

Here is the CSV (technically semi-colon separated) file I'm working with:

\begin{filecontents*}{olive_oil_data.csv}
Code;Product Name;Unit Size;Pack Size;Unit Price
1463;AGULLO OLIVE OIL (Valencia , good value and fruity using Picual, Arbequina, Cornicabra and Chancló olives) ;5 litre;3;£17.00
1489;CAL MAS EXTRA VIRGEN OIL (Valencia, artisan single estate using Blanqueta and Alfafara olives );750ml;12;£8.52
3001;GOMEOLIVA OLIVE OIL (Andalucia, smooth and fruity using Hojiblanca and Picual olives);5 litre;3;£20.25
3002;GOMEOLIVA OLIVE OIL (Andalucia, smooth and fruity using Hojiblanca and Picual olives);2 litre;6;£8.33
3003;GOMEOLIVA OLIVE OIL  (Andalucia, smooth and fruity using Hojiblanca and Picual olives );1 litre;15;£4.13
3008;MAIMONA OLIVE OIL GLASS (Extramadura,  intense green colour using Morisca olives);750ml;12;£3.86
3004;MAIMONA OLIVE OIL TIN (Extramadura, intense green colour using Morisca olives);5 litre;4;£23.91
3016;MAS D'EN GIL OLIVE OIL (Priorat, artisan delicate taste using Arberquina olives );500ml;6;£8.57
3011;MOLINO OLIVE OILGLASS (Andalucia, unfiltered fruity using Hojiblanca and Picual olives );750ml;6;£4.85
3009;MOLINO OLIVE OIL TIN (Andalucia unfiltered fruity, Hojiblanca  Picual olives);5 litre;3;£22.10
3019;ORGANIC OLIVE OIL (Andalucia, organic using Hojiblanca and Picual olives );5 litre;3;£26.93
3020;ORGANIC OLIVE OIL (Andalucia , organic using Hojiblanca and Picual olives);2 litre;6;£11.03
1331;CAPIRETE PICUAL OLIVE OIL (Jaen, single estate high expression oil);500ml;12;£5.77
3021;ORGANIC OLIVE OIL GLASS  (Andalucia, Andalucia, organic using Hojiblanca and Picual olives );1 litre;12;£6.21
\end{filecontents*}

I know it's good practice to provide a MWE, so far all I have is this:

\documentclass[a4paper,11pt]{article}
\usepackage[top=1cm, bottom=1cm, left=1cm, right=1cm]{geometry}
\usepackage{longtable}
\usepackage{csvsimple}
\begin{document}
\section*{Olive Oil}
    \csvautolongtable[
    before reading=\footnotesize,
    late after line=\\\hline, 
    late after last line=, 
    after reading=\normalsize,
    separator=semicolon,
    respect and=true]
    {./olive_oil_data.csv}
  \end{longtable}
\end{document}

Thank you for your help.

  • Welcome to TeX.SX! Thank you for providing a MWE. – Nicola Talbot Mar 16 '15 at 15:17
4

For the pounds symbol, there has to be some file encoding chosen. If you are in the Windows world, you may have cp1252 encoded files (?).

In my answer, I assume this encoding and use latin1 on inputenc. If you use UTF-8, this setting has to be changed.

The following code tries to rebuild your screenshot (but in black and white so far).

enter image description here

\begin{filecontents*}{olive_oil_data.csv}
Code;Product Name;Unit Size;Pack Size;Unit Price
1463;AGULLO OLIVE OIL (Valencia , good value and fruity using Picual, Arbequina, Cornicabra and Chancló olives) ;5 litre;3;£17.00
1489;CAL MAS EXTRA VIRGEN OIL (Valencia, artisan single estate using Blanqueta and Alfafara olives );750ml;12;£8.52
3001;GOMEOLIVA OLIVE OIL (Andalucia, smooth and fruity using Hojiblanca and Picual olives);5 litre;3;£20.25
3002;GOMEOLIVA OLIVE OIL (Andalucia, smooth and fruity using Hojiblanca and Picual olives);2 litre;6;£8.33
3003;GOMEOLIVA OLIVE OIL  (Andalucia, smooth and fruity using Hojiblanca and Picual olives );1 litre;15;£4.13
3008;MAIMONA OLIVE OIL GLASS (Extramadura,  intense green colour using Morisca olives);750ml;12;£3.86
3004;MAIMONA OLIVE OIL TIN (Extramadura, intense green colour using Morisca olives);5 litre;4;£23.91
3016;MAS D'EN GIL OLIVE OIL (Priorat, artisan delicate taste using Arberquina olives );500ml;6;£8.57
3011;MOLINO OLIVE OILGLASS (Andalucia, unfiltered fruity using Hojiblanca and Picual olives );750ml;6;£4.85
3009;MOLINO OLIVE OIL TIN (Andalucia unfiltered fruity, Hojiblanca  Picual olives);5 litre;3;£22.10
3019;ORGANIC OLIVE OIL (Andalucia, organic using Hojiblanca and Picual olives );5 litre;3;£26.93
3020;ORGANIC OLIVE OIL (Andalucia , organic using Hojiblanca and Picual olives);2 litre;6;£11.03
1331;CAPIRETE PICUAL OLIVE OIL (Jaen, single estate high expression oil);500ml;12;£5.77
3021;ORGANIC OLIVE OIL GLASS  (Andalucia, Andalucia, organic using Hojiblanca and Picual olives );1 litre;12;£6.21
\end{filecontents*}

\documentclass[a4paper,11pt]{article}

% Encoding (e.g. Windows Cp1252)
% Has to be changed for UFT-8, if used
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}

\usepackage[top=1cm, bottom=1cm, left=1cm, right=1cm]{geometry}
\usepackage{longtable,array}
\usepackage{csvsimple}

\usepackage{lmodern}% could be left out or chanaged to another font

\newcommand*{\headentry}[2]{\multicolumn{1}{#1}{\centering\arraybackslash\bfseries #2}}

\begin{document}
\section*{Olive Oil}

\csvreader[
  separator=semicolon,
  before table=\sffamily\small,
  longtable={|r|p{10cm}|r|r|r|},
  table head={\hline%
    \headentry{|m{1.8cm}|}{PRODUCT\linebreak CODE}
    & \headentry{m{10cm}|}{PRODUCT NAME}
    & \headentry{m{1cm}|}{UNIT\linebreak SIZE}
    & \headentry{m{1cm}|}{PACK\linebreak SIZE}
    & \headentry{m{2.2cm}|}{WHOLESALE\linebreak UNIT PRICE}\\\hline},
  late after line=\\\hline,
]
{./olive_oil_data.csv}
{Code=\code,Product Name=\name,Unit Size=\unitsize,Pack Size=\packsize,Unit Price=\unitprice}
{\code & \name & \unitsize & \packsize & \unitprice}

\end{document}

Using the booktabs package gives maybe some more pleasing results. See the following example code:

enter image description here

\begin{filecontents*}{olive_oil_data.csv}
Code;Product Name;Unit Size;Pack Size;Unit Price
1463;AGULLO OLIVE OIL (Valencia , good value and fruity using Picual, Arbequina, Cornicabra and Chancló olives) ;5 litre;3;£17.00
1489;CAL MAS EXTRA VIRGEN OIL (Valencia, artisan single estate using Blanqueta and Alfafara olives );750ml;12;£8.52
3001;GOMEOLIVA OLIVE OIL (Andalucia, smooth and fruity using Hojiblanca and Picual olives);5 litre;3;£20.25
3002;GOMEOLIVA OLIVE OIL (Andalucia, smooth and fruity using Hojiblanca and Picual olives);2 litre;6;£8.33
3003;GOMEOLIVA OLIVE OIL  (Andalucia, smooth and fruity using Hojiblanca and Picual olives );1 litre;15;£4.13
3008;MAIMONA OLIVE OIL GLASS (Extramadura,  intense green colour using Morisca olives);750ml;12;£3.86
3004;MAIMONA OLIVE OIL TIN (Extramadura, intense green colour using Morisca olives);5 litre;4;£23.91
3016;MAS D'EN GIL OLIVE OIL (Priorat, artisan delicate taste using Arberquina olives );500ml;6;£8.57
3011;MOLINO OLIVE OILGLASS (Andalucia, unfiltered fruity using Hojiblanca and Picual olives );750ml;6;£4.85
3009;MOLINO OLIVE OIL TIN (Andalucia unfiltered fruity, Hojiblanca  Picual olives);5 litre;3;£22.10
3019;ORGANIC OLIVE OIL (Andalucia, organic using Hojiblanca and Picual olives );5 litre;3;£26.93
3020;ORGANIC OLIVE OIL (Andalucia , organic using Hojiblanca and Picual olives);2 litre;6;£11.03
1331;CAPIRETE PICUAL OLIVE OIL (Jaen, single estate high expression oil);500ml;12;£5.77
3021;ORGANIC OLIVE OIL GLASS  (Andalucia, Andalucia, organic using Hojiblanca and Picual olives );1 litre;12;£6.21
\end{filecontents*}

\documentclass[a4paper,11pt]{article}

% Encoding (e.g. Windows Cp1252)
% Has to be changed for UFT-8, if used
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}

\usepackage[top=1cm, bottom=1cm, left=1cm, right=1cm]{geometry}
\usepackage{longtable,array,booktabs,xcolor}
\usepackage{csvsimple}

\usepackage{lmodern}% could be left out or chanaged to another font

\newcommand*{\headentry}[2]{\multicolumn{1}{m{#1}}{\centering\arraybackslash\textcolor{red!85!black}{\bfseries #2}}}

\begin{document}
\section*{Olive Oil}

\csvreader[
  separator=semicolon,
  before table=\sffamily\small,
  longtable={cp{10cm}rrr},
  table head={\toprule%
    \headentry{1.8cm}{PRODUCT\linebreak CODE}
    & \headentry{10cm}{PRODUCT NAME}
    & \headentry{1cm}{UNIT\linebreak SIZE}
    & \headentry{1cm}{PACK\linebreak SIZE}
    & \headentry{2.2cm}{WHOLESALE\linebreak UNIT PRICE}\\\midrule},
  late after line=\\\midrule,
  late after last line=\\\bottomrule,
]
{./olive_oil_data.csv}
{Code=\code,Product Name=\name,Unit Size=\unitsize,Pack Size=\packsize,Unit Price=\unitprice}
{\code & \name & \unitsize & \packsize & \bfseries\unitprice}

\end{document}
  • That's perfect. I knew LaTeX could do this but I think it was slightly beyond my current understanding. Thank you very much! – OMCS Mar 17 '15 at 14:03

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.