如何在表格单元格内换行并仅在某些情况下在下一列中添加文本?

如何在表格单元格内换行并仅在某些情况下在下一列中添加文本?

我对 LaTeX 还不太熟悉,我正在制作一份酒单。
在某些情况下,我需要添加一杯和/或半瓶的价格……但只有在这些情况下,当有这样的选项时才需要添加。

我在这里找到了一些问题的答案,并选择了\usepackage{makecell}makecell[]{}策略:
如何在表格单元格内添加强制换行符
如何在表格中换行[重复]

这是我的代码:

\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}

这是我目前得到的,对我来说是一个很好的开始: 在单元格中划线以显示价格

但我仍然有一些问题:

  1. 我希望只有当这些价格存在时,才出现带有玻璃杯和/或半瓶咖啡价格以及“玻璃杯”和/或“半瓶咖啡”文字的行。
  2. 第一个价格(通常是一瓶,这是假定的,或者是其他的东西,比如半瓶,50cL,大瓶或双大瓶,在这种情况下是指定的)与葡萄酒的年份+名称的对齐似乎不正确。

=> 我该如何解决这些问题?

我知道我所要求的可能需要使用类似于循环的东西if...else但由于我的原始文件已经很大并且需要一段时间才能编译,因此如果可能的话,我想避免它。如果不行的话...好吧,我会使用它。^^'

这是我用来制作 MWE 的 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

答案1

以下提供您所需要的条件设置:

在此处输入图片描述

\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}

该方法背后的原理是检查\HalfCarafePrice是空的或不。基于此,我们将定义\hcp为空{}或包含价格和tabular换行符\\,然后设置\hcp

我掉了makecell代替[t]op-aligned tabular

相关内容