The csvsimple
manual describes it. This is an modified example from the manual.
\documentclass{standalone}
\usepackage{csvsimple}
\begin{filecontents*}{grade.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}
\begin{document}
\begin{tabular}{l|c}%
\bfseries Person & \bfseries Matr.~No.% specify table head
\csvreader[head to column names]{grade.csv}{}% use head of csv as column names
{\\\hline\givenname\ \name & \matriculation}% specify your coloumns here
\end{tabular}
\end{document}
result is:
If you have spaces in your header you can't use head to column names
. So you may use \csvcoli
etc. Here a quick example
\documentclass{article}
\usepackage{csvsimple}
\begin{filecontents*}{Basket_ball.csv}
Time (s),Zeroed time (s),Y Position in pixels,Zeroed Y Position in pixels,Y Position in meters using original trans. Eq.,Y Position In Meters using new trans. Eq.
43.97,0,734,528,14.22624,18.26294
44.01,0.04,731,525,14.11335,18.14345
44.04,0.07,729,523,14.03819,18.06389
44.07,0.1,726,520,13.9256,17.9447
44.11,0.14,720,514,13.70096,17.70686
44.14,0.17,718,512,13.62624,17.62774
44.17,0.2,714,508,13.47704,17.46974
44.21,0.24,711,505,13.36535,17.35145
44.24,0.27,706,500,13.1796,17.1547
44.27,0.3,700,494,12.95736,16.91926
44.31,0.34,696,490,12.8096,16.7627
\end{filecontents*}
\begin{document}
\section*{Theory}
\begin{tabular}{l|l}%
\bfseries Time (s) & \bfseries Zeroed time (s)% specify table head
\csvreader[head to column names]{Basket_ball.csv}{}% use head of csv as column names
{\\\hline\csvcoli&\csvcolii}% specify your coloumns here
\end{tabular}
\end{document}
This is a problem with fragile commands. See What is the difference between Fragile and Robust commands?
With e-tex compatible compiler you may use \newrobustcmd
from etoolbox
:
\begin{filecontents*}{data.csv}
column1,column2,column3
name1,val1a,val1b
name2,val2a,val2b
\end{filecontents*}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{csvsimple}
\usepackage{etoolbox}
\newrobustcmd{\csvget}[3]{\csvreader[no head, filter equal={\thecsvinputline}{#2}]{#1}{}{#3}}
\begin{document}
\listoftables
\begin{table}
\centering
\csvautotabular{data.csv}
\caption{A caption \csvget{data.csv}{2}{\protect\csvcolii}}
\end{table}
\end{document}
Without it you may use \DeclareRobustCommand
\begin{filecontents*}{data.csv}
column1,column2,column3
name1,val1a,val1b
name2,val2a,val2b
\end{filecontents*}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{csvsimple}
\DeclareRobustCommand{\csvget}[3]{\csvreader[no head, filter equal={\thecsvinputline}{#2}]{#1}{}{#3}}
\begin{document}
\listoftables
\begin{table}
\centering
\csvautotabular{data.csv}
\caption{A caption \csvget{data.csv}{2}{\protect\csvcolii}}
\end{table}
\end{document}
the result is:
Best Answer
Using a unique macro for each column that you want to import, will lead to the desired output:
From the csvsimple manual:
See: Introduction, page 1
See: Macros for the Processing of CSV Files, page 8
If you use the identical macro for two different columns, you assign the contents of the first column to this macro. With the assignment of the contents of the second column, you then overwrite the first assignment. This will then lead to the observed output where all columns with the same macro contain the contents of the last column.