LaTeX longtable 列不匹配

LaTeX longtable 列不匹配

我对 LATEX 非常陌生,我正在用以下代码创建一个表格:

\begin{spreadtab}{{longtable}[t t t]{lp{8.2cm}r}}
  @ \noalign{\vskip 2mm} \textbf{Position} & @ \textbf{Beschreibung} & @ \textbf{Preis in $currency$} \\ \hline
      $for(service)$ @ \noalign{\vskip 2mm} \refstepcounter{pos} \thepos 
        & @ $service.description$ 
        $if(service.details)$\newline \begin{itemize} 
          $for(service.details)$\scriptsize \item $service.details$ 
          $endfor$ \end{itemize}
          $endif$ & $service.price$\\
      $endfor$ \noalign{\vskip 10mm} \hline
  @ & @ \multicolumn{1}{r}{Subtotal:} & :={sum(c1:[0,-1])} \\
  @ & @ \multicolumn{1}{r}{MwSt. $VAT$\%:} & $VAT$/100*[0,-1] \\
  @ & @ \multicolumn{1}{r}{\textbf{Total:}} & \textbf{:={[0,-1]+[0,-2]}} \\
\end{spreadtab}

效果很好!

我想添加另一列并将其更改为:

\begin{spreadtab}{{longtable}[t t t t]{lp{8.2cm}r}}
  @ \noalign{\vskip 2mm} \textbf{Position} & @ \textbf{Beschreibung} & @ \textbf{Test} & @ \textbf{Preis in $currency$} \\ \hline
      $for(service)$ @ \noalign{\vskip 2mm} \refstepcounter{pos} \thepos 
        & @ $service.description$ 
        $if(service.details)$\newline \begin{itemize} 
          $for(service.details)$\scriptsize \item $service.details$ 
          $endfor$ \end{itemize}
          $endif$ & $service.price$ & $service.price$\\
      $endfor$ \noalign{\vskip 10mm} \hline
  @ & @ \multicolumn{1}{r}{Subtotal:} & :={sum(d1:[0,-1])} \\
  @ & @ \multicolumn{1}{r}{MwSt. $VAT$\%:} & $VAT$/100*[0,-1] \\
  @ & @ \multicolumn{1}{r}{\textbf{Total:}} & \textbf{:={[0,-1]+[0,-2]}} \\
\end{spreadtab} 

这导致我出现此错误:

! 额外的对齐标签已更改为 \cr。\endtemplate

l.167 \结束{spreadtab}

我知道这通常意味着每行的列数不匹配,但在这种情况下它们匹配。知道哪里出了问题吗?

编辑:

这是完整的 tex 文件:

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode

\documentclass[$fontsize$, a4paper]{article}

\usepackage{pagecolor}
\usepackage{xcolor}

\definecolor{gray}{HTML}{EEEEEE}


% LAYOUT
%--------------------------------
\usepackage{geometry} 
\geometry{$geometry$}

% No page numbers
\pagenumbering{gobble}

% Left align
\usepackage[document]{ragged2e}

$if(letterhead)$
  \usepackage{wallpaper}
  \ULCornerWallPaper{1}{letterhead.pdf}
$endif$

% TYPOGRAPHY
%--------------------------------
\usepackage{fontspec} 
\usepackage{xunicode}
\usepackage{xltxtra}

% converts LaTeX specials (quotes, dashes etc.) to Unicode
\defaultfontfeatures{Mapping=tex-text}
\setsansfont{$sansfont$}

% Set paragraph break
\setlength{\parskip}{1em}

$if(sansfont)$
  \setsansfont{$sansfont$}
$endif$

% Command required by how Pandoc handles the list conversion
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

% Set Line Height
\renewcommand{\baselinestretch}{1.2}

% TABLE CUSTOMIZATION
%--------------------------------
\usepackage{spreadtab}
\usepackage{longtable}
\usepackage{ltablex}


\usepackage[compact]{titlesec} % For customizing title sections
\titlespacing*{\section}{0pt}{3pt}{-7pt} % Remove margin bottom from the title
\renewcommand{\arraystretch}{1.5} % Apply vertical padding to table cells
\usepackage{hhline} % For single-cell borders
\usepackage{enumitem} % For customizing lists
\setlist{nolistsep} % No whitespace around list items
\setlist[itemize]{leftmargin=0.5cm} % Reduce list left indent
\setlength{\tabcolsep}{9pt} % Larger gutter between columns


% LANGUAGE
%--------------------------------
$if(lang)$
\usepackage{polyglossia}
\setmainlanguage{$lang$}
$endif$

% PDF SETUP
%--------------------------------
\usepackage[xetex, bookmarks, colorlinks, breaklinks]{hyperref}
\hypersetup
{
  pdfauthor=$author$,
  pdfsubject=Invoice Nr. $invoice-nr$,
  pdftitle=Invoice Nr. $invoice-nr$,
  linkcolor=blue,
  citecolor=blue,
  filecolor=black,
  urlcolor=blue
}

% To display custom date
% \usepackage[nodayofweek]{datetime}
% \newdate{date}{01}{12}{1867}
% \date{\displaydate{date}}
% Use this instead of \today: % \displaydate{date}


% DOCUMENT
%--------------------------------
\begin{document}
% \pagecolor{gray}

% From Address
\normalsize \sffamily \scriptsize
$for(from)$
$from$ 
$endfor$

% To Address
\normalsize \sffamily
$for(to)$
$to$\\
$endfor$

\vspace{6em}

\begin{flushright}
  \small
  $city$, \today
\end{flushright}

\vspace{1em}

% Subject
\section*{\sffamily \textsc{Angebot}}

\vspace{1em}

% Offer Details
{Angebot-Nr.:} \hspace{5.2mm} {$invoice-nr$}\\
{Ort, Datum:} \hspace{7mm} {$city$,} \today

\vspace{3em}

% Opening Note
$openingnote$

% Page Break
% \newpage

\footnotesize
\newcounter{pos}
\setcounter{pos}{0}
\STautoround*{2} % Get spreadtab to always display the decimal part
\STsetdecimalsep{,} % Use comma as decimal separator

% \begin{spreadtab}{{longtable}[t t t]{lp{8.2cm}r}}
%   @ \noalign{\vskip 2mm} \textbf{Position} & @ \textbf{Beschreibung} & @ \textbf{Preis in $currency$} \\ \hline
%       $for(service)$ @ \noalign{\vskip 2mm} \refstepcounter{pos} \thepos 
%         & @ $service.description$ 
%         $if(service.details)$ \newline \begin{itemize} 
%           $for(service.details)$\scriptsize \item $service.details$ 
%           $endfor$ \end{itemize}
%           $endif$ & $service.price$\\
%       $endfor$\noalign{\vskip 10mm} \hline
%   @ & @ \multicolumn{1}{r}{Zwischensumme:} & :={sum(c1:[0,-1])} \\
%   @ & @ \multicolumn{1}{r}{MwSt. $VAT$\%:} & $VAT$/100*[0,-1] \\
%   @ & @ \multicolumn{1}{r}{\textbf{Gesamt:}} & \textbf{:={[0,-1]+[0,-2]}} \\
% \end{spreadtab}

\begin{spreadtab}{{longtable}[t t t t]{lp{8.2cm}r}}
  @ \noalign{\vskip 2mm} \textbf{Position} & @ \textbf{Beschreibung} & @ \textbf{Test} & @ \textbf{Preis in $currency$} \\ \hline
      $for(service)$ @ \noalign{\vskip 2mm} \refstepcounter{pos} \thepos 
        & @ $service.description$ 
        $if(service.details)$\newline \begin{itemize} 
          $for(service.details)$\scriptsize \item $service.details$ 
          $endfor$ \end{itemize}
          $endif$ & $service.price$ & $service.price$\\
      $endfor$ \noalign{\vskip 10mm} \hline
  @ & @ \multicolumn{1}{r}{Subtotal:} & :={sum(d1:[0,-1])} \\
  @ & @ \multicolumn{1}{r}{MwSt. $VAT$\%:} & $VAT$/100*[0,-1] \\
  @ & @ \multicolumn{1}{r}{\textbf{Total:}} & \textbf{:={[0,-1]+[0,-2]}} \\
\end{spreadtab} 

\vspace{3em}

% Legal Note
$legalnote$

\vspace{3em}

% Closing Note
$closingnote$

\vspace{1em}

% Signature
\includegraphics[height=2.5\baselineskip]{signature.pdf} \par

$author$

% \medskip

% $author$

\end{document}

答案1

我们不知道您处理队列和变量,例如$\fontsize$。因此,上面的代码不是一个有效的示例(尝试xelatex …文件,您会看到我们的问题)。不过,您似乎没有理解的可选参数和强制参数longtable

可选参数,[t t t][t t t t]在您的示例中是整个表格(而不是列)的水平对齐参数。t在那里是未知的,并且序列也是未知的t,这将导致表格的默认对齐。请参阅longtable手动的关于有效的对齐选项lr以及c

强制参数{lp{8.2cm}r}是列声明。l是左对齐且无换行符的列,p{8.2cm}\parbox宽度为 8.2cm 的列,r是右对齐且无换行符的列。 有关列声明的更多信息,请参阅 LaTeX 简介。

因此,要添加更多列,您必须扩展列声明,而不是已经错误使用的整个表的可选水平对齐参数。例如,您可以减少 -column 的 withp并添加另一个r-column:

\begin{spreadtab}{{longtable}{lp{8.2cm}rr}}
  @ \noalign{\vskip 2mm} \textbf{Position} & @ \textbf{Beschreibung} & @ \textbf{Test} & @ \textbf{Preis in $currency$} \\ \hline
      $for(service)$ @ \noalign{\vskip 2mm} \refstepcounter{pos} \thepos 
        & @ $service.description$ 
        $if(service.details)$\newline \begin{itemize} 
          $for(service.details)$\scriptsize \item $service.details$ 
          $endfor$ \end{itemize}
          $endif$ & $service.price$ & $service.price$\\
      $endfor$ \noalign{\vskip 10mm} \hline
  @ & @ \multicolumn{1}{r}{Subtotal:} & :={sum(d1:[0,-1])} \\
  @ & @ \multicolumn{1}{r}{MwSt. $VAT$\%:} & $VAT$/100*[0,-1] \\
  @ & @ \multicolumn{1}{r}{\textbf{Total:}} & \textbf{:={[0,-1]+[0,-2]}} \\
\end{spreadtab} 

我只能想象第一行的这个改变会解决你的问题,因为我无法测试它。

答案2

您的问题表述不当。要弄清楚您用来pandoc生成 pdf 的内容,您提供的“完整代码”实际上是 pandoc 的模板(甚至包括用于创建循环的迷你语言,例如 等),真是令人费解$for(service)$。所有这些值都$variables必须在外部 YAML 文件中提供,等等。

您在评论中给出的链接https://github.com/mrzool/invoice-boilerplate提供了拼图中缺失的部分,使我能够从您的模板中生成一个真正的“最小工作示例”(嗯,不是真正的“最小”,当然也不是“工作”,但您明白了)。

经过一些调整后,我终于得到了预期的(我希望!)结果:

结果

为了最终实现这一目标,我必须做到以下几点:

  1. template.tex使用您在问题中提供的代码创建一个名为的文件。
  2. details.yml创建名为复制自的文件https://github.com/mrzool/invoice-boilerplate/blob/master/details.yml,并将该行更改sansfont: Helvetica Neuesansfont: "Nimbus Sans L",因为我的系统中没有 Helvetica Neue。
  3. 注释掉你的行\includegraphics[height=2.5\baselineskip]{signature.pdf} \par,因为我没有该文件signature.pdf
  4. .tex使用以下方式创建文件pandoc details.yml -o output.tex --template=template.tex
  5. 使用 编译结果xelatex output.tex,以获得上面显示的最终 pdf。

经过所有这些,我终于有了一个“最小非工作示例”,展示了您在问题中描述的问题。记下我必须猜测才能到达这里的所有信息,您就会明白为什么我以“您的问题表述不正确”开头。

此后,解决方案很简单,它确实是 Schweinebacke 猜测的那个,即r在 之后添加第二个lp{8.2cm}r,原因由 Schweinebacke 解释。

仅供参考,这是.tex由 pandoc 生成的,在使用以下方式编译时会生成上面显示的图像xelatex

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode

\documentclass[10pt, a4paper]{article}

\usepackage{pagecolor}
\usepackage{xcolor}

\definecolor{gray}{HTML}{EEEEEE}


% LAYOUT
%--------------------------------
\usepackage{geometry} 
\geometry{a4paper, left=43mm, right=43mm, top=51mm, bottom=17mm}

% No page numbers
\pagenumbering{gobble}

% Left align
\usepackage[document]{ragged2e}


% TYPOGRAPHY
%--------------------------------
\usepackage{fontspec} 
\usepackage{xunicode}
\usepackage{xltxtra}

% converts LaTeX specials (quotes, dashes etc.) to Unicode
\defaultfontfeatures{Mapping=tex-text}
\setsansfont{Nimbus Sans L}

% Set paragraph break
\setlength{\parskip}{1em}

  \setsansfont{Nimbus Sans L}

% Command required by how Pandoc handles the list conversion
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

% Set Line Height
\renewcommand{\baselinestretch}{1.2}

% TABLE CUSTOMIZATION
%--------------------------------
\usepackage{spreadtab}
\usepackage{longtable}
\usepackage{ltablex}


\usepackage[compact]{titlesec} % For customizing title sections
\titlespacing*{\section}{0pt}{3pt}{-7pt} % Remove margin bottom from the title
\renewcommand{\arraystretch}{1.5} % Apply vertical padding to table cells
\usepackage{hhline} % For single-cell borders
\usepackage{enumitem} % For customizing lists
\setlist{nolistsep} % No whitespace around list items
\setlist[itemize]{leftmargin=0.5cm} % Reduce list left indent
\setlength{\tabcolsep}{9pt} % Larger gutter between columns


% LANGUAGE
%--------------------------------
\usepackage{polyglossia}
\setmainlanguage{english}

% PDF SETUP
%--------------------------------
\usepackage[xetex, bookmarks, colorlinks, breaklinks]{hyperref}
\hypersetup
{
  pdfauthor=Max Mustermann,
  pdfsubject=Invoice Nr. 2015-11-04,
  pdftitle=Invoice Nr. 2015-11-04,
  linkcolor=blue,
  citecolor=blue,
  filecolor=black,
  urlcolor=blue
}

% To display custom date
% \usepackage[nodayofweek]{datetime}
% \newdate{date}{01}{12}{1867}
% \date{\displaydate{date}}
% Use this instead of \today: % \displaydate{date}


% DOCUMENT
%--------------------------------
\begin{document}
% \pagecolor{gray}

% From Address
\normalsize \sffamily \scriptsize
Musterstraße 37 
12345 Musterstadt 

% To Address
\normalsize \sffamily
Erika Mustermann\\
Musterallee 1\\
12345 Musterstadt\\
Germany\\

\vspace{6em}

\begin{flushright}
  \small
  Musterstadt, \today
\end{flushright}

\vspace{1em}

% Subject
\section*{\sffamily \textsc{Angebot}}

\vspace{1em}

% Offer Details
{Angebot-Nr.:} \hspace{5.2mm} {2015-11-04}\\
{Ort, Datum:} \hspace{7mm} {Musterstadt,} \today

\vspace{3em}

% Opening Note


% Page Break
% \newpage

\footnotesize
\newcounter{pos}
\setcounter{pos}{0}
\STautoround*{2} % Get spreadtab to always display the decimal part
\STsetdecimalsep{,} % Use comma as decimal separator

% \begin{spreadtab}{{longtable}[t t t]{lp{8.2cm}r}}
%   @ \noalign{\vskip 2mm} \textbf{Position} & @ \textbf{Beschreibung} & @ \textbf{Preis in EUR} \\ \hline
%        @ \noalign{\vskip 2mm} \refstepcounter{pos} \thepos 
%         & @ The first service provided 
%          & 320.0\\
%        @ \noalign{\vskip 2mm} \refstepcounter{pos} \thepos 
%         & @ And another one, with a list of details 
%          \newline \begin{itemize} 
%           \scriptsize \item Some more detailed explanation 
%           \scriptsize \item of the service provided 
%           \scriptsize \item Looking good 
%            \end{itemize}
%            & 245.0\\
%        @ \noalign{\vskip 2mm} \refstepcounter{pos} \thepos 
%         & @ The last service provided 
%          & 65.0\\
%       \noalign{\vskip 10mm} \hline
%   @ & @ \multicolumn{1}{r}{Zwischensumme:} & :={sum(c1:[0,-1])} \\
%   @ & @ \multicolumn{1}{r}{MwSt. 20\%:} & 20/100*[0,-1] \\
%   @ & @ \multicolumn{1}{r}{\textbf{Gesamt:}} & \textbf{:={[0,-1]+[0,-2]}} \\
% \end{spreadtab}

\begin{spreadtab}{{longtable}[t t t]{lp{8.2cm}rr}}
  @ \noalign{\vskip 2mm} \textbf{Position} & @ \textbf{Beschreibung} & @ \textbf{Test} & @ \textbf{Preis in EUR} \\ \hline
       @ \noalign{\vskip 2mm} \refstepcounter{pos} \thepos 
        & @ The first service provided 
         & 320.0 & 320.0\\
       @ \noalign{\vskip 2mm} \refstepcounter{pos} \thepos 
        & @ And another one, with a list of details 
        \newline \begin{itemize} 
          \scriptsize \item Some more detailed explanation 
          \scriptsize \item of the service provided 
          \scriptsize \item Looking good 
           \end{itemize}
           & 245.0 & 245.0\\
       @ \noalign{\vskip 2mm} \refstepcounter{pos} \thepos 
        & @ The last service provided 
         & 65.0 & 65.0\\
       \noalign{\vskip 10mm} \hline
  @ & @ \multicolumn{1}{r}{Subtotal:} & :={sum(d1:[0,-1])} \\
  @ & @ \multicolumn{1}{r}{MwSt. 20\%:} & 20/100*[0,-1] \\
  @ & @ \multicolumn{1}{r}{\textbf{Total:}} & \textbf{:={[0,-1]+[0,-2]}} \\
\end{spreadtab} 

\vspace{3em}

% Legal Note


\vspace{3em}

% Closing Note
Please transfer the due amount to the following bank account within the
next 14 days:

Mustermann GmbH\\
Kreditinstitut: Deutsche Postbank AG\\
IBAN: DE18 3601 0043 9999 9999 99\\
BIC: PBNKDEFF\\
We really appreciate your business and look forward to future projects
together. Best regards,

\vspace{1em}

% Signature
%\includegraphics[height=2.5\baselineskip]{signature.pdf} \par

Max Mustermann

% \medskip

% Max Mustermann

\end{document}

我不明白为什么你不接受他的答案,而是提供赏金。你试过他提出的解决方案了吗?无论如何,我认为 Schweinebacke 值得赏金,因为他给出了正确的答案。

相关内容