更新(2019 年 1 月)

更新(2019 年 1 月)

在 TeX 论坛上,我询问了一种以方便的方式存储变量的方法,这样非技术人员就不会害怕使用 LaTeX。为了将其提升到一个新的水平,我想在文件中定义文档(在本例中为发票)的完整结构,.cls包括文本输出。

这实际上是不可能的,因为您不允许在命令之前打印内容\begin{document}

我想要的是两个文档文件夹设置。invoice.cls文档类文件,预格式化所有内容,以及main.tex最终用户可以输入上一个问题中所示的变量并设置账单的文件。

对于账单,我定义了newenvironment一些自定义命令和计数器,以便 LaTeX 计算总金额并打印在页面上。

理想情况下main.tex应该只包含这两件事:

\documentclass{invoice}

\begin{document}

    \StoreInvoiceData{%
        %% %%%%%%
        %% Sender
        %% %%%%%%
        Tagline     =   {Hello World},
        FirstName   =   {Victor K.},
        LastName    =   {Driscoll},
        Street      =   {1616 Hill Croft Farm Road},
        Zip         =   {95932},
        Town        =   {Colusa},
        Country     =   {USA},
        IBAN        =   {YY24KIHB12476423125915947930915268},
        SWIFT       =   {ANTSUB4L},

        %% %%%%%%%%
        %% Receiver
        %% %%%%%%%%
        ClientFirstName     =   {John},
        ClientLastName      =   {Doe},
        ClientStreet        =   {123 Main St},
        ClientZip           =   {43221},
        ClientTown          =   {Anytown},
        ClientCountry       =   {USA}
    }

    \begin{invoicetable}
        %% %%%%%%%
        %% Hosting
        %% %%%%%%%
        \feetype{Hosting Expenses}
        \unitrow{Test 1}{1}{10}    % \unitrow{Name}{Quantity}{Item price}
        \unitrow{Test 2}{1}{15}
        \unitrow{Test 3}{1}{20}
        \subtotal

        %% %%%%%%%%%%%%
        %% Maintainance
        %% %%%%%%%%%%%%
        \feetype{Maintenance Fees}
        \unitrow{Test 4}{2}{15}
        \unitrow{Test 5}{1}{20}
        \unitrow{Test 6}{5}{25}
        \unitrow{Test 7}{1}{30}
        \subtotal
    \end{invoicetable}

\end{document}

应该invoice.cls处理所有其余部分。如果在 -list 中未输入任何值\StoreInvoiceData,则应打印空白字段。该类应自动打印标题(一个 TikZ 矩形,其中包含名称和标语的节点)、地址字段(也是一个放置在正确坐标处以匹配信封窗口的 TikZ 矩形)等等。

目前,这一切都是通过\newcommands然后在中调用来完成main.tex的,但是正如我所说的,我想尽可能保持该文件干净,并且我确信可以在 LaTeX 类中定义完整的布局。


如果有什么不清楚的话,请随时询问。

谢谢您的帮助,我非常感激!

答案1

这是我前段时间编写的一个标准美国商业发票类的示例。它为发票文档定义了一些用户命令,并从 CSV 文件中获取发票本身的值。该类本身格式化了所有内容,用户文档非常简单。

有多种方法可以使其变得更好,但它应该足以让您了解如何完成这种事情。

更新(2019 年 1 月)

我现在已将其打包为文档类,支持多页发票、退货地址配置文件和文档。它可在加拿大运输安全局并包含在 TeX Live 和 MikTeX 中。

invoice-class

% US Commericial Invoice Class
% Copyright 2008-2017 by Alan Munn [email protected]
%
% This class may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This package has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this package is Alan Munn [email protected]
%
\def\invc@date{2008/07/15}
\def\invc@version{1.0}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{invoice-class}[\invc@date\space  Commercial Invoice \invc@version]
\LoadClass[11pt]{article}
\RequirePackage[margin=1in]{geometry}
\RequirePackage{datatool}
\RequirePackage{multicol}
\RequirePackage{array}
\RequirePackage{tabularx}
\RequirePackage{dcolumn}
\RequirePackage{fancyhdr}
\renewcommand{\rmdefault}{\sfdefault}
\newcommand*{\invc@BL}[1]{\rule{#1}{.5pt}}
\newcommand*\invc@NL{\tabularnewline}
\newcolumntype{d}{D{.}{.}{6.2}}
\newcommand*{\invc@mc}{\multicolumn{1}{c|}}
\newcommand*{\invc@waybill}{
\fbox{\textbf{Waybill Number:} \invc@waybillnum}}
\newcommand*{\waybill}[1]{\gdef\invc@waybillnum{#1}}
\newcommand{\toaddress}[1]{\gdef\invc@toaddress{#1}}
\newcommand*{\destination}[1]{\gdef\invc@destination{#1}}
\newcommand*{\carrier}[1]{\gdef\invc@carrier{#1}}
\newcommand*{\InputFile}[1]{\gdef\invc@inputfile{#1}}
\newcommand*{\weight}[2]{\gdef\invc@pounds{#1}\gdef\invc@oz{#2}}
\newcommand*{\packages}[1]{\gdef\invc@packages{#1}}
\let\invc@shippingdate\today
% These could be made into user commands but if they are fixed, they can simply be
% hard coded as is done here
\newcommand{\invc@fromaddress}{%
The Duck Company\\
Some Avenue\\
Somewhere MI 48823\\
USA\\
Tel. 555-555-5555\\
}
\newcommand*{\invc@shipper}{Prof. van Duck}
\newcommand*{\invc@location}{Somewhere, MI}
%
\pagestyle{fancy}
\parindent=0pt
\lhead{}
\chead{\Large \sffamily \bfseries Commercial Invoice }
\rhead{}
\lfoot{}
\cfoot{\sffamily\thepage}
\rfoot{}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0pt}
\thispagestyle{fancy}
\sffamily
\newcommand{\printinvoice}{%
\invc@waybill
\begin{multicols}{2}

{\Large\bfseries From:}

\invc@fromaddress

\columnbreak

{\Large\bfseries To:}

\invc@toaddress
\end{multicols}

\setlength{\extrarowheight}{9pt}
\begin{tabularx}{\linewidth}{|*{4}{>{\centering}X|}}
\hline
\textbf{\scriptsize Country of ultimate destination:} & \textbf{\scriptsize Exporting Carrier} & \textbf{\scriptsize Total number of packages} & \textbf{\scriptsize Gross Weight} \invc@NL
\invc@destination & \invc@carrier &
\invc@packages & \invc@pounds~lb \invc@oz~oz \invc@NL
\hline
\end{tabularx}
%
\DTLloaddb{invoice}{\invc@inputfile}
\DTLsumcolumn{invoice}{Amount}{\invc@total}
\setlength{\extrarowheight}{3pt}
\begin{tabularx}{\linewidth}{|X|p{1in}|p{.75in}|d|d|}
\hline
\centering\bfseries Description & \centering\bfseries Country of Origin & \centering \bfseries Quantity &\invc@mc{\bfseries Unit Value\$} & \invc@mc{\bfseries Amount (USD)}\invc@NL
\hline
\DTLforeach{invoice}{%
\description=Description,\country=Country,\quantity=Quantity,\unitvalue=UnitValue,\amount=Amount}{%
\description & \country & \quantity & \unitvalue & \amount\\} & & & & \\\hline
\multicolumn{4}{|r|}{\bfseries Currency type and amount}& \DTLdecimaltocurrency{\invc@total}{\invc@subtotal}\invc@subtotal   \invc@NL
\multicolumn{4}{|r|}{\bfseries Packing cost} & \invc@NL
\multicolumn{4}{|r|}{\bfseries Shipping cost} & \invc@NL
\multicolumn{4}{|r|}{\bfseries Insurance cost} & \invc@NL
\hline\hline
\multicolumn{4}{|r|}{\bfseries Total Invoice Value} & \DTLdecimaltocurrency{\invc@total}{\invc@grandtotal}\invc@grandtotal \invc@NL
\hline
\end{tabularx}
\vskip 5ex
{\Large\bfseries Notes:}

{\footnotesize These commodities, technologies, or software were exported from the United States of
America in accordance with the Export Administration regulations. Diversion contrary to
U.S. law is prohibited.

It is hereby certified that this invoice shows: (1) the actual price of the goods described,
(2) that no other invoice has been or will be issued, and (3) that all particulars are true
and correct.}

\setlength{\extrarowheight}{18pt}
\begin{tabular}{|p{3in}|p{1.75in}|}
\hline
\textbf{\scriptsize Name:} \hskip 2em \invc@shipper & \textbf{\scriptsize Date:} \invc@shippingdate \invc@NL

\hline
\multicolumn{2}{|l|}{\textbf{\scriptsize Location:} \hskip 1em \invc@location}\invc@NL
\hline
\multicolumn{2}{|l|}{ \textbf{\scriptsize Signature:}}\invc@NL
\hline
\end{tabular}
}
\endinput

用户文档示例

这是示例用户文档。我已使用 CSV 文件将其包含在内filecontents,但它通常是一个外部文件。

\documentclass{invoice-class}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.csv}
Description, Country, Quantity, UnitValue, Amount
Yellow Ducks, USA, 5, 2.00, 10.00
Pink Ducks, USA, 4, 15.00, 60.00
Blue Ducks, USA, 6, 5.00, 30.00
Orange Ducks, USA, 1, 4.00, 4.00
Red Ducks, USA, 30, 2.00, 60.00
\end{filecontents*}
\InputFile{\jobname.csv}
\waybill{8273205776}
\carrier{FedEx}
\destination{Brazil}
\toaddress{Paulo Cereda\\Rua Bonita\\Cidade de Patos\\SP\\Brazil}
\weight{1}{1}
\packages{1}
\begin{document}
\printinvoice
\end{document}

输出

在此处输入图片描述

相关内容