2

I am quite new to LaTeX and I am making a wine menu.
In some cases I need to add a price for a single glass and/or for a half-carafe... But ONLY in these cases, when such an option exists.

I found some answers to my questions here, and opted for the \usepackage{makecell} and makecell[]{} strategy:
How to add a forced line break inside a table cell
How to break a line in a table [duplicate]

Here is my code:

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

% ----- TO MODIFY MARGINS -----%%
\usepackage{geometry}
% Marges du document
 \geometry{hmargin=0.25cm,vmargin=2.5cm}
%\usepackage[left=0.5cm, right=0cm, top=2.5cm, bottom=2.5cm]{geometry}

% To change the MARGINS with \begin{adjustwidth}{Xcm}{Xcm}
\usepackage{changepage}

% DATATOOL package to load and create external data files (csv)
\usepackage{datatool}

% ------ To make TABLES ------%%
\usepackage{array}

\usepackage{tabularx,ragged2e}

% Pour ajuster les tables trop grandes
\usepackage{adjustbox}

% To force a line break in a cell
\usepackage{makecell}

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

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

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

\title{Test to break line in a table}
\author{CB}
\date{October 2018}

\begin{document}

\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Iteration for Coravin
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\noindent
\begin{tabularx}{\linewidth}{
    % @{} % no \tabcolsep on the left side
    >{\RaggedRight}p{0.65cm} % SizeCode
    % @{} % no \tabcolsep
    >{\RaggedRight}p{2cm} % Vintage
    X % Name Classification NameB Grower & Origin
    % @{} % no \tabcolsep
    >{\RaggedLeft}p{1.25cm} % Price
    >{\RaggedRight}p{1.9cm} % SizeBis
    @{} % no \tabcolsep on the right side
}%
    \DTLforeach*[\DTLiseq{\Coravin}{X}] % Condition
    {WIN} % Database label
    {\Vintage=Vintage,\Name=Name,\Classification=Classification,\NameB=NameB,\Grower=Grower,\Origin=(Origin),\Region=Region,\Subregion=Subregion,\SellPrice=SellPrice,\GlassPrice=GlassPrice,\HalfCarafePrice=HalfCarafePrice,\Coravin=Coravin,\Bin=Bin,\SizeBis=SizeBis} % Assignment
    {% Stuff to do at each iteration:
    \makecell[l]{\small{\textbf{\Bin}} \\ \\} &
    \makecell[l]{\textbf{\Vintage} \\ \\} &
    \makecell[l]{\textbf{\Name}
    \textit{\Classification}
    \textbf{\NameB} \textbf{\Grower} \\ \\} &
    \makecell[l]{\textit{\pounds\SellPrice} \\ \textit{\pounds\HalfCarafePrice} \\ \textit{\pounds\GlassPrice}} &
    \makecell[l]{\footnotesize{\SizeBis} \\ \footnotesize{Half Carafe} \\ \footnotesize{Glass}} \\
    }%
\end{tabularx}%
%%%%%%%%%%%%%%%%%%%%%%%%%


\end{document}

And this is what I get so far, which is a good beginning to me: Break line in cell for prices

However I still have some issues:

  1. I would like the lines with the glass and/or half-carafe prices and the "Glass" an/or "Half-Carafe" text to appear ONLY when these prices exist.
  2. The alignment of the first price (usually a bottle, which is assumed, or something else such as a Half, a 50cL, a Magnum or a Double Magnum, which in this case is specified) with the Vintage + Name of the wine doesn't seem to be right.

=> How do I solve these problems?

I am aware that what I am asking for might require the use of something like a loop with if and else... But as my original file is already very big and takes a while to compile I would like to avoid it IF POSSIBLE. If not then... Well, I'd use it. ^^'

Here is the csv excerpt which I used to make this MWE:

Vintage,Name,Classification,NameB,Grower,Quantity,Origin,(Origin),Area,Region,Subregion,Type,Wine,Bin,Xsizes,SizeBis,SizeCode,UnitPerCase,Coef,CostPrice,SellPrice,SellPriceB,ClubPrice,HalfCarafePrice,GlassPrice,HouseWines,LowStock,Coravin
2011,La Réserve de Léoville Barton,2ème vin de Château Léoville Barton,,,,Saint-Julien,(Saint-Julien),Saint-Julien,R03,R03,Red,Bordeaux,,,,,12,1,20.86,34,34,,,X,,,X
2009,Château Poujeaux,Cru Bourgeois Exceptionnel,,,,Moulis-en-Médoc,(Moulis-en-Médoc),Médoc,R06,R06,Red,Bordeaux,,,,,12,1,26.62,47,47,,,X,,,X
2009,Château Dassault,Grand Cru,,,,Saint-Émilion,(Saint-Émilion),Saint-Émilion,R07,R07,Red,Bordeaux,,,,,12,1,32.49,59,59,,,X,,,X
2005,Royal Tokaji Aszu 5 Puttonyos,,,,,Hungary,(Hungary),Sweet Wines,R11,R11,Sweet,ROW,950,,50 cl,,24,2,13.75,29,58,29,,6,,,X
2001,Château Lafaurie-Peyraguey,1er Grand Cru Classé,,,,Sauternes,(Sauternes),Sweet Wines,R11,R11,Sweet,Bordeaux,139,O,Half,H,24,2,14.00,35,70,,,13,,,X
2011,Fonseca,Late Bottled Vintage Port,,,,Portugal,(Portugal),Port,R12,R12,Red,Port,1400,,,,12,1,9.72,22,22,22,11,4,,,X
1998,Fonseca Guimaraens,Bottled in 2000,,,,Portugal,(Portugal),Port,R12,R12,Red,Port,1404,,,,12,1,16.50,34,34,34,18,7,,,X
20 year old,Taylor's,Tawny Port,,,,Portugal,(Portugal),Port,R12,R12,Red,Port,1405,,,,12,1,23.29,46,46,46,,8,,,X
1994,Croft,,,,,Portugal,(Portugal),Port,R12,R12,Red,Port,1401,,,,12,1,30.00,50,50,65,25,10,,,X
2009,Gevrey-Chambertin,,Champs Cheny,Dom. Philippe Roty,,Gevrey-Chambertin,(Gevrey-Chambertin),Côte-de-Nuits,R22,R220,Red,Burgundy,,,,,12,1,39.67,60,60,,,X,,,X
2015,Santenay Blanc,,,Dom. Bachelet Monnot,,Santenay,(Santenay),Côte-de-Beaune,R31,R313,White,Burgundy,655,,,,12,1,20.50,35,35,40,,X,,,X

1 Answers1

2

The following provides the conditional setting you need:

enter image description here

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{Coravin.csv}
Vintage,Name,Classification,NameB,Grower,Quantity,Origin,(Origin),Area,Region,Subregion,Type,Wine,Bin,Xsizes,SizeBis,SizeCode,UnitPerCase,Coef,CostPrice,SellPrice,SellPriceB,ClubPrice,HalfCarafePrice,GlassPrice,HouseWines,LowStock,Coravin
2011,La Réserve de Léoville Barton,2ème vin de Château Léoville Barton,,,,Saint-Julien,(Saint-Julien),Saint-Julien,R03,R03,Red,Bordeaux,,,,,12,1,20.86,34,34,,,X,,,X
2009,Château Poujeaux,Cru Bourgeois Exceptionnel,,,,Moulis-en-Médoc,(Moulis-en-Médoc),Médoc,R06,R06,Red,Bordeaux,,,,,12,1,26.62,47,47,,,X,,,X
2009,Château Dassault,Grand Cru,,,,Saint-Émilion,(Saint-Émilion),Saint-Émilion,R07,R07,Red,Bordeaux,,,,,12,1,32.49,59,59,,,X,,,X
2005,Royal Tokaji Aszu 5 Puttonyos,,,,,Hungary,(Hungary),Sweet Wines,R11,R11,Sweet,ROW,950,,50 cl,,24,2,13.75,29,58,29,,6,,,X
2001,Château Lafaurie-Peyraguey,1er Grand Cru Classé,,,,Sauternes,(Sauternes),Sweet Wines,R11,R11,Sweet,Bordeaux,139,O,Half,H,24,2,14.00,35,70,,,13,,,X
2011,Fonseca,Late Bottled Vintage Port,,,,Portugal,(Portugal),Port,R12,R12,Red,Port,1400,,,,12,1,9.72,22,22,22,11,4,,,X
1998,Fonseca Guimaraens,Bottled in 2000,,,,Portugal,(Portugal),Port,R12,R12,Red,Port,1404,,,,12,1,16.50,34,34,34,18,7,,,X
20 year old,Taylor's,Tawny Port,,,,Portugal,(Portugal),Port,R12,R12,Red,Port,1405,,,,12,1,23.29,46,46,46,,8,,,X
1994,Croft,,,,,Portugal,(Portugal),Port,R12,R12,Red,Port,1401,,,,12,1,30.00,50,50,65,25,10,,,X
2009,Gevrey-Chambertin,,Champs Cheny,Dom. Philippe Roty,,Gevrey-Chambertin,(Gevrey-Chambertin),Côte-de-Nuits,R22,R220,Red,Burgundy,,,,,12,1,39.67,60,60,,,X,,,X
2015,Santenay Blanc,,,Dom. Bachelet Monnot,,Santenay,(Santenay),Côte-de-Beaune,R31,R313,White,Burgundy,655,,,,12,1,20.50,35,35,40,,X,,,X
\end{filecontents*}

% ----- TO MODIFY MARGINS -----%%
\usepackage[margin=1in]{geometry}

\usepackage{datatool}
\usepackage{tabularx,ragged2e}

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

\begin{document}

\noindent
\begin{tabularx}{\linewidth}{
    % @{} % no \tabcolsep on the left side
    >{\RaggedRight}p{0.65cm} % SizeCode
    % @{} % no \tabcolsep
    >{\RaggedRight}p{2cm} % Vintage
    X % Name Classification NameB Grower & Origin
    % @{} % no \tabcolsep
    >{\RaggedLeft}p{1.25cm} % Price
    >{\RaggedRight}p{1.9cm} % SizeBis
    @{} % no \tabcolsep on the right side
}%
    \DTLforeach*[\DTLiseq{\Coravin}{X}] % Condition
      {WIN} % Database label
      {\Vintage=Vintage,
       \Name=Name,
       \Classification=Classification,
       \NameB=NameB,
       \Grower=Grower,
       \Origin=(Origin),
       \Region=Region,
       \Subregion=Subregion,
       \SellPrice=SellPrice,
       \GlassPrice=GlassPrice,
       \HalfCarafePrice=HalfCarafePrice,
       \Coravin=Coravin,
       \Bin=Bin,
       \SizeBis=SizeBis} % Assignment
      {% Stuff to do at each iteration:
        \small\bfseries \Bin &
        \bfseries \Vintage   &
        {\bfseries \Name}
        {\itshape \Classification}
        {\bfseries \NameB{} \Grower} &
        \itshape\begin{tabular}[t]{@{} l @{}}
          \pounds\SellPrice \\
          % https://tex.stackexchange.com/q/53068/5764
          \expandafter\if\expandafter\relax\expandafter\detokenize\expandafter{\HalfCarafePrice}\relax
            \def\hcp{}%
          \else
            \def\hcp{\pounds\HalfCarafePrice \\}%
          \fi
          \hcp
          \pounds\GlassPrice
        \end{tabular} &
        \begin{tabular}[t]{@{} l @{}}
          \strut\footnotesize \SizeBis \\
          % https://tex.stackexchange.com/q/53068/5764
          \strut\expandafter\if\expandafter\relax\expandafter\detokenize\expandafter{\HalfCarafePrice}\relax
            \def\hcp{}%
          \else
            \def\hcp{\footnotesize Half Carafe \\}%
          \fi
          \hcp
          \strut\footnotesize Glass
        \end{tabular} \\
      }%
\end{tabularx}

\end{document}

The principle behind the approach is to check whether \HalfCarafePrice is empty or not. Based on this, we define \hcp to be either empty {} or contain the price and a tabular newline \\ and then set \hcp.

I dropped makecell in lieu of a [t]op-aligned tabular.

Werner
  • 603,163
  • Thank you! I'll have to read a bit to understand how this works exactly, but it does the job perfectly! I also notice many differences with my code, in particular you changed textbf into bfseries and textit into itshape. For example \small{\textbf{\Bin}} became \small\bfseries \Bin and \textit{\Classification} became \itshape \Classification. As both codes seem to work, why did you change it? Is it a new syntax and mine is obsolete? – Claire Boitet Oct 29 '18 at 19:33
  • 1
    Each cell within a tabular forms a group, so issuing \bfseries or \itshape is limited by the terminating & or \\. Also, one should use {\small ...}, not \small{...} since \small is a font declaration, not a macro that takes an argument. With all that I thought I'd save some keystrokes by removing the braces {...} as well. You can use \textbf{...} or \textit{...} or {\bfseries ...} or {\itshape ...}. – Werner Oct 29 '18 at 19:38
  • You also changed \textbf{\NameB} \textbf{\Grower} for {\bfseries \NameB{} \Grower}... Why is there a pair of curly braces {} after the \NameB? (Sorry if these questions are trite or trivial, I'm really a beginner. ^^' ) – Claire Boitet Oct 29 '18 at 20:01
  • 1
  • I implemented this solution in my document, and checked if the "GlassPrice" cell was empty as well, and it works wonders. :) HOWEVER I still have a small issue: the spacing between the lines has now increased and is the same as if I had 2 prices (Bottle + Glass typically) even if there is only 1... How could I prevent this? (I'd like to show a picture here if I could, but apparently it's not possible in the comments.) – Claire Boitet Oct 31 '18 at 13:59
  • 1
    @ClaireBoitet: Edit your post, upload the image but don't save the edit. Just copy the image link and paste that in a comment. – Werner Oct 31 '18 at 14:08
  • Nice trick! Here are my pictures:

    The normal spacing before I added the Glass and Half-Carafe prices: https://i.stack.imgur.com/84IaR.png The increased spacing after I added the Glass and Half-Carafe prices: https://i.stack.imgur.com/1fwAG.png

    – Claire Boitet Oct 31 '18 at 16:45
  • I actually found the solution by myself, and it was quite an easy one actually: I just put the line break symbol \ at the beginning of the Glass and Half-Carafe price and text instead of after the Bottle and Half-Carafe ones and I didn't put any after the Glass price and text: \pounds\SellPrice \ > \pounds\SellPrice, \def\hcp{\pounds\HalfCarafePrice \}% > \def\hcp{\ \pounds\HalfCarafePrice}%, \def\gp{\pounds\GlassPrice \}% > \def\gp{\ \pounds\GlassPrice}%, \strut\footnotesize \SizeBis \ > \strut\footnotesize \SizeBis, etc. (I tried to format with ` but it did odd things with \ ) – Claire Boitet Nov 01 '18 at 13:12