对于不习惯 LaTeX 语法的人来说,准备 Csv 文件可能更容易。我想使用包含以下内容的 csv 文件
Id、我的作者、我的日期、我的地址
- 塞巴斯蒂安,2012 年 12 月 12 日,XYZ 路,XYZ 城市
- 玫瑰,2013 年 12 月 12 日,XYZ 路,abc 城市
我必须以这样的方式定义一个命令,如果我提供 Id 的输入,则以下示例中的相应字段应该自动填充。
\documentclass{article}
\usepackage{eso-pic}
\usepackage{fancyhdr}
\usepackage{blindtext} % just for the example
\usepackage[headsep=3cm,top=5cm]{geometry}
\AddToShipoutPicture{%
\AtTextUpperLeft{%
\makebox(420,75)[lt]{%
\footnotesize%
\begin{tabular}{@{}*{3}{p{4.5cm}}@{}}%
\textbf{Author}\newline\myauthor&%
\textbf{Date of birth}\newline\mydate&%
\textbf{Address}\newline\myaddress%
\end{tabular}%
}}}
\pagestyle{fancy}
\newcommand{\myauthor}{user34083}
\newcommand{\mydate}{December 26, 1997}
\newcommand{\myaddress}{26 Washington Ave., Manhattan, New York.\newline United States of America}
\begin{document}
\blinddocument % just for the example
\end{document}
答案1
这是一个可能的解决方案:
\documentclass{article}
\usepackage{eso-pic}
\usepackage{fancyhdr}
\usepackage{blindtext} % just for the example
\usepackage[headsep=3cm,top=5cm]{geometry}
\usepackage{datatool}
% create sample csv file
\begin{filecontents*}{test.csv}
Id,myauthor,mydate,myaddress
1,Sebastian,12.12.2012,XYZ road,XYZ city
2,Rose,12.12.2013,XYZ road,abc city
\end{filecontents*}
% load data
\DTLloaddb{mydata}{test.csv}
\AddToShipoutPicture{%
\AtTextUpperLeft{%
\makebox(420,75)[lt]{%
\footnotesize%
\begin{tabular}{@{}*{3}{p{4.5cm}}@{}}%
\textbf{Author}\newline\myauthor&%
\textbf{Date of birth}\newline\mydate&%
\textbf{Address}\newline\myaddress%
\end{tabular}%
}}}
\pagestyle{fancy}
\newcommand{\myauthor}{user34083}
\newcommand{\mydate}{December 26, 1997}
\newcommand{\myaddress}{26 Washington Ave., Manhattan, New York.\newline United States of America}
% Define a command that fetches data for the row with the ID
% given in the argument
\newcommand*{\fetchdata}[1]{%
% fetch the first matching row
\dtlgetrowforvalue{mydata}{\dtlcolumnindex{mydata}{Id}}{#1}%
% Lookup the required values from this row
\dtlgetentryfromcurrentrow{\myauthor}{\dtlcolumnindex{mydata}{myauthor}}%
\dtlgetentryfromcurrentrow{\mydate}{\dtlcolumnindex{mydata}{mydate}}%
\dtlgetentryfromcurrentrow{\myaddress}{\dtlcolumnindex{mydata}{myaddress}}%
}
% fetch data for Id=2
\fetchdata{2}
\begin{document}
\blinddocument % just for the example
\end{document}
这将产生(仅页面顶部):
编辑:
在上面的示例中,城市与道路位于不同的列中,因此它不会出现在文档中。如果您希望街道和城市都位于同一列中myaddress
,请记住在 CSV 文件中对包含逗号的条目使用双引号分隔符:
Id,myauthor,mydate,myaddress
1,Sebastian,12.12.2012,"XYZ road, XYZ city"
2,Rose,12.12.2013,"XYZ road, abc city"
或者,添加额外的标题并进行\fetchdata
相应修改。例如,编辑 CSV 文件,使其如下所示:
Id,myauthor,mydate,myroad,mycity
1,Sebastian,12.12.2012,XYZ road,XYZ city
2,Rose,12.12.2013,XYZ road,abc city
并更改\fetchdata
为:
\newcommand*{\fetchdata}[1]{%
\dtlgetrowforvalue{mydata}{\dtlcolumnindex{mydata}{Id}}{#1}%
\dtlgetentryfromcurrentrow{\myauthor}{\dtlcolumnindex{mydata}{myauthor}}%
\dtlgetentryfromcurrentrow{\mydate}{\dtlcolumnindex{mydata}{mydate}}%
\dtlgetentryfromcurrentrow{\myroad}{\dtlcolumnindex{mydata}{myroad}}%
\dtlgetentryfromcurrentrow{\mycity}{\dtlcolumnindex{mydata}{mycity}}%
\renewcommand*{\myaddress}{\myroad, \mycity}%
}