![csvsimple 和 tabularray 不兼容吗?](https://linux22.com/image/436151/csvsimple%20%E5%92%8C%20tabularray%20%E4%B8%8D%E5%85%BC%E5%AE%B9%E5%90%97%EF%BC%9F.png)
csvsimple
和新包之间是否存在不兼容性tabularray
?以下 MWE 会引发错误。(使用 XeLaTeX 或 LuaLaTeX 进行编译)
\documentclass{article}
\usepackage[ngerman,french]{babel}
\usepackage{fontspec}
\usepackage{tabularray} %needs package ninecolors.sty
\usepackage{booktabs}
\usepackage{csvsimple}
\begin{filecontents*}{data.csv}
frA,frB,deA
un cogne,arg.,ein Bulle
morfler,arg.,eins abkriegen
\end{filecontents*}
\begin{document}
This is what I'd like to obtain, with environment \verb|tblr|:
\noindent
\begin{tblr}{colspec={X[2,l]X[4,l]},
width=0.5\linewidth,
hlines,
}
\textit{un cogne} (arg.)& \textit{un policier ou un gendarme}\\
\textit{morfler} (arg.)& {\selectlanguage{ngerman}\textit{eins abkriegen}}\\
\end{tblr}
\bigskip
A normal tabular seems to work (but I'd miss the fonctions of tabularray...)
(not only the X column type, also the alignment facilities):
% \csvreader
\noindent
\begin{tabular}{p{4cm}p{8cm}}\toprule
\csvreader[
late after line=\\\midrule,
late after last line=\\\bottomrule,
head to column names,
]{data.csv}{}{%
\textit{\frA} (\frB) & \textit{\deA}
}
\end{tabular}
\bigskip
But this throws an error:
\noindent
\begin{tblr}{
colspec={X[2,l]X[4,l]},
width=0.5\linewidth,
hlines,
}
\csvreader[
late after line=\\\midrule,
late after last line=\\\bottomrule,
head to column names,
]{data.csv}{}{%
\textit{\frA} (\frB) & \textit{\deA}
}
\end{tblr}
\end{document}
答案1
更新:
与csvsimple
v2.1.0 (2021/07/06)csvsimple-l3
兼容tabularray
。
您应该使用\usepackage[l3]{csvsimple}
或\usepackage{csvsimple-l3}
来加载csvsimple-l3
,而不是被取代的csvsimple-legacy
。
\begin{filecontents*}{data.csv}
frA,frB,deA
un cogne,arg.,ein Bulle
morfler,arg.,eins abkriegen
\end{filecontents*}
\documentclass{article}
\usepackage[ngerman,french]{babel}
\usepackage{fontspec}
\usepackage{tabularray} %needs package ninecolors.sty
\usepackage{booktabs}
\usepackage[l3]{csvsimple} % or "\usepackage{csvsimple-l3}"
\begin{document}
\subsection*{Desired output}
This is what I'd like to obtain, with environment \verb|tblr|:
\begin{tblr}{
colspec={X[2,l]X[4,l]},
width=0.5\linewidth,
hlines,
}
\textit{un cogne} (arg.) & \textit{un policier ou un gendarme}\\
\textit{morfler} (arg.) & {\selectlanguage{ngerman}\textit{eins abkriegen}}\\
\end{tblr}
\subsection*{With \texttt{tabular} env}
A normal tabular seems to work (but I'd miss the functions of tabularray...)
(not only the X column type, also the alignment facilities):
\begin{tabular}{p{4cm}p{8cm}}\toprule
\csvreader[
late after line=\\\midrule,
late after last line=\\\bottomrule,
head to column names,
]{data.csv}{}{%
\textit{\frA} (\frB) & \textit{\deA}
}
\end{tabular}
\subsection*{With \texttt{tblr} env}
% required by tabularray
\NewTableCommand\toprule{\hline[0.08em]}
\NewTableCommand\midrule{\hline[0.05em]}
\NewTableCommand\bottomrule{\hline[0.08em]}
\csvreader[
tabularray={
colspec={X[2,l]X[4,l]},
width=0.5\linewidth,
% hlines
},
table head=\toprule,
late after line=\\\midrule,
late after last line=\\\bottomrule,
head to column names,
]{data.csv}{}{%
\noexpand\textit{\frA} (\frB) & \noexpand\textit{\deA}
}
\end{document}
输出与下面相同。
原来的:
这是第一次尝试csvsimple
。想法是\csvloop
(按照 的口味csvsimple-legacy
)首先将整个表格谱表存储在宏中(此处\csv@output
),而不是直接打印它们。然后用户可以用来\csvoutput
打印表格。
受限于tabularray
实现方式,每个\hline
类似谱表、、\\
和&
都应该在中直接看到\csv@output
。
\begin{filecontents*}{data.csv}
frA,frB,deA
un cogne,arg.,ein Bulle
morfler,arg.,eins abkriegen
\end{filecontents*}
\documentclass{article}
\usepackage[ngerman,french]{babel}
\usepackage{fontspec}
\usepackage{tabularray} %needs package ninecolors.sty
\usepackage{booktabs}
\usepackage{csvsimple}
%%%
%%% patch for csvsimple-lagency
%%%
\usepackage{xpatch}
\makeatletter
\newif\ifcsv@storeonly
\csvset{
% new keys
table env/.store in=\csv@tableenv,
store only/.is if=csv@storeonly,
% redefinition
tabular/.style={
@table={\csv@pretable\begin{\csv@tableenv}{#1}\csv@tablehead}{\csv@tablefoot\end{\csv@tableenv}\csv@posttable},
late after line=\\
},
% init new keys
table env=tabular,
store only=false
}
% store-or-print patch for \csvloop
\long\def\csvloop#1{%
% reset
\global\let\csv@output\@empty
\global\let\@endloophook\csv@empty%
\global\let\csvlinetotablerow\csv@assemble@csvlinetotablerow%
% options
\csvset{default,every csv,#1}%
\csv@preprocss%
\csv@set@catcodes%
\csv@print@or@ostore\csv@prereading%
\csv@print@or@ostore\csv@table@begin%
\setcounter{csvinputline}{0}%
% start reading
\openin\csv@file=\csv@input@filename\relax%
\ifeof\csv@file%
\csv@error{File '\csv@input@filename' not existent, not readable, or empty!}{}%
\else%
% the head line
\csv@opt@processheadline%
\fi%
%
\setcounter{csvrow}{0}%
\gdef\csv@do@preline{%
\csv@print@or@ostore\csv@prefirstline%
\gdef\csv@do@preline{\csv@print@or@ostore\csv@preline}%
}%
\gdef\csv@do@postline{%
\csv@print@or@ostore\csv@postfirstline%
\gdef\csv@do@postline{\csv@print@or@ostore\csv@postline}%
}%
\gdef\csv@do@@latepostline{%
\csv@print@or@ostore\csv@latepostfirstline%
\gdef\csv@do@latepostline{\csv@print@or@ostore\csv@latepostline}%
}%
\gdef\csv@do@latepostline{%
\csv@print@or@ostore\csv@lateposthead%
\global\let\csv@do@latepostline=\csv@do@@latepostline%
}%
% command for the reading loop
\gdef\csv@iterate{%
\let\csv@usage=\csv@empty%
\csvreadnext%
\ifeof\csv@file%
\global\let\csv@next=\csv@empty%
\else%
\global\let\csv@next=\csv@iterate%
\ifx\csv@par\csvline\relax%
\else%
\csv@escanline{\csvline}%
% check and decide
\csv@opt@checkcolumncount%
\fi%
\fi%
% do or do not
\csv@usage%
\csv@next}%
\ifeof\csv@file%
\global\let\csv@next=\csv@empty%
\else%
\global\let\csv@next=\csv@iterate%
\fi%
\csv@next%
\closein\csv@file%
\@endloophook%
\csv@print@or@ostore\csv@latepostlastline%
\csv@print@or@ostore\csv@table@end%
\csv@print@or@ostore\csv@postreading%
\csv@reset@catcodes%
}
\newcommand\csvoutput{%
% expose tabular-like env name
\xpatchcmd@self\csv@output\csv@tableenv
\xpatchcmd@self\csv@output\csv@tableenv
% expose hrule-like commands
\xpatchcmd@self\csv@output\csv@tablehead
\xpatchcmd@self\csv@output\csv@tablefoot
% output
\csv@output
}
% helper, \xpatchcmd#1{#2}{<one-step expansion of #2>}{}{\fail}
\newcommand\xpatchcmd@self[2]{%
\expanded{\unexpanded{\xpatchcmd#1{#2}}{\unexpanded\expandafter{#2}}}{}{\fail}%
}
% store-or-print patch for \csv@do@linecommand
\xpatchcmd\csv@do@linecommand
{\csv@body}
{\csv@print@or@estore\csv@body}
{}{\fail}
% expand once
\def\csv@print@or@ostore#1{\csv@print@or@store@meta
{\expandafter\g@addto@macro\expandafter\csv@output\expandafter{#1}}
{#1}}
% fully expansion
\def\csv@print@or@estore#1{\csv@print@or@store@meta
{\expanded{\noexpand\g@addto@macro\noexpand\csv@output{#1}}}
{#1}}
\def\csv@print@or@store@meta{%
\ifcsv@storeonly
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\makeatother
%%%
%%% end of patch
%%%
\begin{document}
\subsection*{Desired output}
This is what I'd like to obtain, with environment \verb|tblr|:
\begin{tblr}{
colspec={X[2,l]X[4,l]},
width=0.5\linewidth,
hlines,
}
\textit{un cogne} (arg.) & \textit{un policier ou un gendarme}\\
\textit{morfler} (arg.) & {\selectlanguage{ngerman}\textit{eins abkriegen}}\\
\end{tblr}
\subsection*{With \texttt{tabular} env}
A normal tabular seems to work (but I'd miss the functions of tabularray...)
(not only the X column type, also the alignment facilities):
\begin{tabular}{p{4cm}p{8cm}}\toprule
\csvreader[
late after line=\\\midrule,
late after last line=\\\bottomrule,
head to column names,
]{data.csv}{}{%
\textit{\frA} (\frB) & \textit{\deA}
}
\end{tabular}
\subsection*{With \texttt{tblr} env}
\csvreader[
table env=tblr,
tabular={
colspec={X[2,l]X[4,l]},
width=0.5\linewidth,
% hlines
},
store only,
table head=\toprule,
late after line=\\\midrule,
late after last line=\\\bottomrule,
head to column names,
]{data.csv}{}{%
\noexpand\textit{\frA} (\frB) & \noexpand\textit{\deA}
}
\NewTableCommand\toprule{\hline[0.08em]}
\NewTableCommand\midrule{\hline[0.05em]}
\NewTableCommand\bottomrule{\hline[0.08em]}
\csvoutput
\end{document}