How to import data from excel and format it as text in LaTeX?

1,032

I'm back and I found a partial solution to my problem.
Please keep in mind that I am almost a complete beginner so I'm experimenting. ^^' (I'm using Overleaf to write my code because I tried on RStudio but the datatool package was apparently not available for R version 3.5.1)

So to simplify, this is the pattern I used:

% Load CSV database (here database.csv)
% and give it a label (here DB)
\DTLloaddb{DB}{database.csv}

%%%%%%%%%%%%%%%%
% Iteration
% Here in the D column I can have d1, d2, etc. 
% but I only want to display the result for the rows with d1:
\DTLforeach*[\DTLiseq{\D}{d1}] % Condition
 {DB} % Database label
{\A=A,\B=B,\C=C,\D=D,\E=E} % Assignment
{% Stuff to do at each iteration:
    \item[]
    \textbf{\A}
    \textbf{ \B}
    \textit{ \C}
    (\E)
}
%%%%%%%%%%%%%%%% 

In my particular situation, I am doing a wine list/menu (Carte des Vins) and eventually I want to:

1) sort them by wine (Bordeaux / Burgundy / Port / Rest of the World)
For now I'm using a csv file with only the Bordeaux wines (bordeaux.csv) but ultimately I would like to use a file with all the wines on it (wine.csv)

2) sort them by type (Red / White / Sweet white)
As you will see below, I managed to do that, but maybe there is a better way to do the same thing.

3) format the text as follows:
Vintage Name, Classification (Origin)
ex: 2009 Château Grand Village, Bordeaux Supérieur (Bordeaux)
The only thing that I have trouble with is the comma, because when the Classification column is empty I don't want to get "Vintage Name," with a useless comma... So for now I left the text without a comma.

So this is my whole code:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}

% Pour modifer les marges
\usepackage{geometry}
% Marges du document 
\geometry{hmargin=2.5cm,vmargin=1.5cm}


% To remove the heading of the table of contents ("Contents")
\makeatletter
\renewcommand\tableofcontents{%
    \@starttoc{toc}%
}
\makeatother

% To make itemized lists
% This package provides user control over the layout of the three basic list 
% environments: enumerate, itemize and description. It supersedes both enumerate 
% and mdwlist (providing well-structured replacements for all their funtionality), 
% and in addition provides functions to compute the layout of labels, and to 
% ‘clone’ the standard environments, to create new environments with counters of 
% their own.
\usepackage{enumitem}

% Datatool package to load external files (cdv)
\usepackage{datatool}

\Huge\title{Carte des Vins}
\author{Buck's}
\date{September 2018}

\begin{document}
    \maketitle
    \tableofcontents

%--------------------------%
\newpage
\section{Bordeaux} 

% Load CSV database and give it a name
\DTLloaddb{BOR}{bordeaux.csv}

    \subsection{Red}
%%%%%%%%%%%%%%%%
% Iteration
\DTLforeach*[\DTLiseq{\Type}{Red}] % Condition
 {BOR} % Database label
{\Vintage=Vintage,\Name=Name,\Classification=Classification,\Type=Type,\Origin=Origin} % Assignment
{% Stuff to do at each iteration:
    \item[]
    \textbf{\Vintage}
    \textbf{ \Name}
    \textit{ \Classification}
    (\Origin)
}
%%%%%%%%%%%%%%%% 

    \subsection{White}
%%%%%%%%%%%%%%%%
% Iteration
\DTLforeach*[\DTLiseq{\Type}{White}] % Condition
 {BOR} % Database label
{\Vintage=Vintage,\Name=Name,\Classification=Classification,\Type=Type,\Origin=Origin} % Assignment
{% Stuff to do at each iteration:
    \item[]
    \textbf{\Vintage}
    \textbf{ \Name}
    \textit{ \Classification}
    (\Origin)
}
%%%%%%%%%%%%%%%% 

    \subsection{Sweet White} 
%%%%%%%%%%%%%%%%
% Iteration
\DTLforeach*[\DTLiseq{\Type}{Sweet white}] % Condition
 {BOR} % Database label
{\Vintage=Vintage,\Name=Name,\Classification=Classification,\Type=Type,\Origin=Origin} % Assignment
{% Stuff to do at each iteration:
    \item[]
    \textbf{\Vintage}
    \textbf{ \Name}
    \textit{ \Classification}
    (\Origin)
}
%%%%%%%%%%%%%%%%


%--------------------------%
\newpage
\section{Burgundy} 
    \subsection{Red}
    \subsection{White}

%--------------------------%
\newpage
\section{Rest of the World} 

%--------------------------%
\newpage
\section{Port}

%--------------------------------

\end{document}

And this is what I managed to get so far (this is the beginning of my section "Bordeaux" and of my subsection "Red"): This is the beginning of my section "Bordeaux" and of my subsection "Red"

Share:
1,032

Related videos on Youtube

Claire Boitet
Author by

Claire Boitet

I am a biologist, science and technology enthusiast, a skeptic as well, who initially registered on StackExchange to learn more about LaTeX and R... but I'm sure there is much more to do here, and I a always keen to learn new things and simplify my life. If I'm wrong just explain me why; if you're convincing and rigorous I'll be happy to change my opinion. I am French but I mainly use English IRL and online (I live in London as well), and I also speak and write a bit of Japanese, although this language is very tricky to master. よろしくお願いします! ^^

Updated on August 01, 2022

Comments

  • Claire Boitet
    Claire Boitet over 1 year

    I have a big database spreadsheet on Excel which is a list of products and I want to use its data to write formatted text in LaTeX.

    The table looks like this:
    Year / Name product / Details
    2000 / AAAAA / aaaaa
    1975 / BBBBB / bbbbb
    1998 / CCCCC / ccccc
    ...

    I would like to use the data from this file in latex and get a text result resembling this (not a table):
    2000 AAAAA, aaaaa
    1975 BBBBB, bbbbb
    1998 CCCCC, ccccc
    ...

    What would be the best solution?
    Should I use excel2latex, csvsimple, something else?
    It reminds me of bibliography paper citations, but I've not been using LaTeX for years and I really don't remember anything useful at the moment.
    I plan to start learning LaTeX again from scratch, but I thought that a very simple way of getting the result I want probably existed and that it would save me hours of research to ask to the community. ^^'

    • Alan Munn
      Alan Munn about 5 years
      If you want to read the file and the data will be kept updated in the Excel file, then you should keep the file in CSV format and then use a package to import it. The datatool package has the most flexibility, and might be most appropriate; other tools like csvsimple and pgfplotstable have more limited functionality and may or may not serve your needs. There are plenty of related questions on the site about these packages. If you're importing the data as a one-time thing, then excel2latex might be appropriate.
    • Azor Ahai -him-
      Azor Ahai -him- about 5 years
      I would open it as a CSV in R and use xtable.
    • Nicola Talbot
      Nicola Talbot about 5 years
      Another possibility is to use datatooltk as in this example.
    • Claire Boitet
      Claire Boitet about 5 years
      Thanks, I'll start by reading about datatool and datatooltk, they seem to be quite appropriate to my needs.
  • Jeffrey J Weimer
    Jeffrey J Weimer about 5 years
    Nicely done for a beginner! I am always loathe to jump to \makeatletter approaches. You might find better hints here.