我有一个 CSV 文件,其中包含姓名、号码和可选图片的列表。此文件将显示在多列表中,其中姓名(以及可选附加的图片作为图标)位于一列,号码位于另一列,第三列为一个空闲列。重要的是,图片/图标直接附加到姓名后面,而不是位于单独的列中。
为了实现这一点,我尝试结合关于动态多列表格的先前问题的答案和这个答案关于使用 CSV 中的其他值对列进行后处理但无法产生预期的结果;以下是我作为 MWE 的尝试:
\documentclass[a4paper]{extarticle}
\usepackage{lmodern}
\usepackage{graphicx}
\graphicspath{ {images/} }
\usepackage[left=1.2cm,right=1.2cm,top=2cm,bottom=1cm]{geometry}
\usepackage{tabularx}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.11}
\begin{filecontents*}{data.csv}
Name,Number,Picture
name1,1,
name2,2,
name3,3,ex1.png
name4,4,
name5,5,
name6,6,
name7,7,
name8,8,
name9,9,ex2.png
name10,10,
name11,11,
\end{filecontents*}
\pgfplotstableread[col sep=comma]{data.csv}{\datatable}
\begin{document}
\pgfplotstableset{create on use/XXX/.style={create col/set={}}}%
\noindent
\pgfplotstabletypeset[string type,
begin table=\begin{tabularx}{\textwidth},
end table=\end{tabularx},
every head row/.style={output empty row,after row=\hline},
every last row/.style={after row=\hline},
columns={Name,Number,XXX,Name,Number,XXX},
display columns/0/.style={select equal part entry of={0}{2},column type=|l,postproc cell content/.add code={%
\pgfplotstablegetelem{\pgfplotstablerow}{Picture}\of\datatable%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{%
\noexpand%
\pgfkeysvalueof{/pgfplots/table/@preprocessed cell content}%
~\pgfplotsretval%
%~\includegraphics[height=\lineheight]{\pgfplotsretval}%
}%
}%
},
display columns/1/.style={select equal part entry of={0}{2}},
display columns/2/.style={column type=X|},
display columns/3/.style={select equal part entry of={1}{2},column type=l,postproc cell content/.add code={%
\pgfplotstablegetelem{\pgfplotstablerow}{Picture}\of\datatable%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{%
\noexpand%
\pgfkeysvalueof{/pgfplots/table/@preprocessed cell content}%
~\pgfplotsretval%
%~\includegraphics[height=\lineheight]{\pgfplotsretval}%
}%
}%
},
display columns/4/.style={select equal part entry of={1}{2}},
display columns/5/.style={column type=X|}
]{\datatable}
\end{document}
如果取消注释这些\includegraphics
语句,代码甚至无法编译。我猜需要检查当前行的图片列中是否有数据;除此之外,还需要可靠地获取当前内容,因为我目前得到的内容总是被抓取"datatable
为内容,尽管它至少可以编译。
我已经尝试多次解决这个问题,但即使在搜索引擎和 PGFPlotsTable 文档的帮助下,我似乎也无法解决这个相当奇怪的问题,所以我很感激任何形式的帮助。
最终解决方案;接受的答案适用于多柱 MWE
这与我现在使用的代码非常接近,并且是接受的答案以及我在问题中给出的多列 MWE。
\documentclass[a4paper]{extarticle}
\usepackage{lmodern}
\usepackage{graphicx}
\graphicspath{ {images/} }
\usepackage[left=1.2cm,right=1.2cm,top=2cm,bottom=1cm]{geometry}
\usepackage{tabularx}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.11}
\usepackage{graphicx}
\graphicspath{ {images/} }
\begin{filecontents*}{data.csv}
Name,Number,Picture
name1,1,
name2,2,
name3,3,ex1.png
name4,4,
name5,5,
name6,6,
name7,7,
name8,8,
name9,9,ex2.png
name10,10,
name11,11,
\end{filecontents*}
\pgfplotstableread[col sep=comma]{data.csv}{\datatable}
\def\zz\ignorespaces#1\unskip{%
\ifx\count#1\count
\gdef\temp{}%
\else
\gdef\temp{%
\kern.2em%
\smash{\raisebox{-.3em}{\includegraphics[height=1.2em]{#1}}}}%
\fi}
\begin{document}
\pgfplotstableset{create on use/XXX/.style={create col/set={}}}%
\noindent
\pgfplotstabletypeset[string type,
begin table=\begin{tabularx}{\textwidth},
end table=\end{tabularx},
every head row/.style={output empty row,after row=\hline},
every last row/.style={after row=\hline},
columns={Picture,Name,Number,XXX,Picture,Name,Number,XXX},
display columns/0/.style={select equal part entry of={0}{2},column type = @{}>{\zz}l@{} },
display columns/1/.style={select equal part entry of={0}{2},column type=|l<{\temp}},
display columns/2/.style={select equal part entry of={0}{2}},
display columns/3/.style={column type=X|},
display columns/4/.style={select equal part entry of={1}{2},column type = @{}>{\zz}l@{} },
display columns/5/.style={select equal part entry of={1}{2},column type=l<{\temp}},
display columns/6/.style={select equal part entry of={1}{2}},
display columns/7/.style={column type=X|}
]{\datatable}
\end{document}
答案1
您可以将图像名称放入表格列中,如果不为空,则只需在表格中挑选它即可在下一列中排版。
\documentclass[a4paper]{extarticle}
\usepackage{lmodern}
\usepackage{graphicx}
\graphicspath{ {images/} }
\usepackage[left=1.2cm,right=1.2cm,top=2cm,bottom=1cm]{geometry}
\usepackage{tabularx}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.11}
\begin{filecontents*}{data.csv}
Name,Number,Picture
name1,1,
name2,2,
name3,3,ex1.png
name4,4,
name5,5,
name6,6,
name7,7,
name8,8,
name9,9,ex2.png
name10,10,
name11,11,
\end{filecontents*}
\pgfplotstableread[col sep=comma]{data.csv}{\datatable}
\def\zz\ignorespaces#1\unskip{%
\ifx\count#1\count
\gdef\temp{}%
\else
\gdef\temp{%
\kern.2em%
\smash{\raisebox{-.3em}{\includegraphics[height=1.7em]{#1}}}}%
\fi}
\begin{document}
\pgfplotstableset{create on use/XXX/.style={create col/set={}}}%
\noindent
\pgfplotstabletypeset[string type,
begin table=\begin{tabularx}{\textwidth},
end table=\end{tabularx},
every head row/.style={output empty row,after row=\hline},
every last row/.style={after row=\hline},
columns={Picture,Name,Number,XXX},
display columns/0/.style={column type = @{}>{\zz}l@{} },
display columns/1/.style={column type = l<{\temp} },
display columns/2/.style={column type = l },
display columns/3/.style={column type=X|},
]{\datatable}
\end{document}