csv 中分号的问题

csv 中分号的问题

使用管道作为分隔符csvsimple

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[frenchb]{babel}
\usepackage{microtype}
\usepackage{csvsimple}
\usepackage{filecontents}

\begin{document}

\begin{filecontents*}{mydata.csv}
  A|B|C|D
  a1|b1|c1|d1
  a2| "b21 and b22"|c2|d2
\end{filecontents*}

\begin{tabular}{c|c|c|c}
  \csvreader[separator=pipe, head to column names, late after line=\\]%
  {mydata.csv}{}%
  {\texttt{\A} & \B & \textbf{\C} & \D}
\end{tabular}

\end{document}

在此处输入图片描述

使用分号作为分隔符csvsimple

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[frenchb]{babel}
\usepackage{microtype}
\usepackage{csvsimple}
\usepackage{filecontents}

\begin{document}

\begin{filecontents*}{mydata2.csv}
  A;B;C;D
  a1;b1;c1;d1
  a2; "b21 and b22";c2;d2
\end{filecontents*}

\begin{tabular}{c|c|c|c}
  \csvreader[separator=semicolon, head to column names, late after line=\\]%
  {mydata2.csv}{}%
  {\texttt{\A} & \B & \textbf{\C} & \D}
\end{tabular}

\end{document}

给我一个错误:

> Running `LaTeX' on `csv' with ``pdflatex  -file-line-error  
> -interaction=nonstopmode "\input" csv.tex'' This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian) (preloaded format=pdflatex)  restricted \write18 enabled. entering extended mode
> LaTeX2e <2016/02/01> Babel <3.9q> and hyphenation patterns for 12
> language(s) loaded. (./csv.tex
> (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document
> Class: article 2014/09/29 v1.4h Standard LaTeX document class
> (/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
> (/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty
> (/usr/share/texlive/texmf-dist/tex/latex/base/utf8.def
> (/usr/share/texlive/texmf-dist/tex/latex/base/t1enc.dfu)
> (/usr/share/texlive/texmf-dist/tex/latex/base/ot1enc.dfu)
> (/usr/share/texlive/texmf-dist/tex/latex/base/omsenc.dfu)))
> (/usr/share/texlive/texmf-dist/tex/latex/base/fontenc.sty
> (/usr/share/texlive/texmf-dist/tex/latex/base/t1enc.def))
> (/usr/share/texmf/tex/latex/lm/lmodern.sty)
> (/usr/share/texlive/texmf-dist/tex/generic/babel/babel.sty
> (/usr/share/texlive/texmf-dist/tex/generic/babel-french/frenchb.ldf
> (/usr/share/texlive/texmf-dist/tex/generic/babel/babel.def)))
> (/usr/share/texlive/texmf-dist/tex/latex/carlisle/scalefnt.sty)
> (/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
> (/usr/share/texlive/texmf-dist/tex/latex/microtype/microtype.sty
> (/usr/share/texlive/texmf-dist/tex/latex/microtype/microtype-pdftex.def)
> (/usr/share/texlive/texmf-dist/tex/latex/microtype/microtype.cfg))
> (/usr/share/texlive/texmf-dist/tex/latex/csvsimple/csvsimple.sty
> (/usr/share/texlive/texmf-dist/tex/latex/pgf/utilities/pgfkeys.sty
> (/usr/share/texlive/texmf-dist/tex/generic/pgf/utilities/pgfkeys.code.tex
> (/usr/share/texlive/texmf-dist/tex/generic/pgf/utilities/pgfkeysfiltered.code.tex)))
> (/usr/share/texlive/texmf-dist/tex/latex/base/ifthen.sty))
> (/usr/share/texlive/texmf-dist/tex/latex/filecontents/filecontents.sty)
> (./csv.aux) (/usr/share/texmf/tex/latex/lm/t1lmr.fd)
> (/usr/share/texlive/texmf-dist/tex/latex/microtype/mt-cmr.cfg)
> 
> LaTeX Warning: Overwriting file `./mydata2.csv'.
> 
> 
> (/usr/share/texmf/tex/latex/lm/ot1lmr.fd)
> (/usr/share/texmf/tex/latex/lm/omllmm.fd)
> (/usr/share/texmf/tex/latex/lm/omslmsy.fd)
> (/usr/share/texmf/tex/latex/lm/omxlmex.fd) ./csv.tex:21: Missing
> \endcsname inserted. <to be read again> 
>                    \penalty  l.21   {\texttt{\A} & \B & \textbf{\C} & \D}
>                                              ./csv.tex:21: Missing \endcsname inserted. <to be read again> 
>                    \penalty  l.21   {\texttt{\A} & \B & \textbf{\C} & \D}
>                                              ./csv.tex:21: Missing $ inserted. <inserted text> 
>                 $ l.21   {\texttt{\A} & \B & \textbf{\C} & \D}
>                                              ./csv.tex:21: Extra \endcsname. \pgfkeyslet ...ter \let \csname pgfk@#1\endcsname 
>                                                   #2 l.21   {\texttt{\A} & \B & \textbf{\C} & \D}
>                                              ) Runaway argument? \pgfkeyssetvalue {/csv head/A\penalty \@M \hskip .16667em \relax
> ;B\penalty \ETC. ! File ended while scanning use of \pgfkeys@temp.
> <inserted text> 
>                 \par  <*> \input csv.tex
>                    ! Emergency stop. <*> \input csv.tex
>                    !  ==> Fatal error occurred, no output PDF file produced! Transcript written on csv.log.
> 
> TeX Output exited abnormally with code 1 at Thu Mar 22 09:02:52

答案1

这是因为[french]{babel}会弄乱角色。不过,您可以使用 / 禁用/;启用此功能,因此以下内容有效:\shorthandoff{;}\shorthandon{;}

在此处输入图片描述

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[frenchb]{babel}
\usepackage{microtype}
\usepackage{csvsimple}
\usepackage{filecontents}

\begin{document}
\begin{filecontents*}{mydata.csv}
  A|B|C|D
  a1|b1|c1|d1
  a2| "b21 and b22"|c2|d2
\end{filecontents*}

\begin{tabular}{c|c|c|c}
  \csvreader[separator=pipe, head to column names, late after line=\\]%
  {mydata.csv}{}%
  {\texttt{\A} & \B & \textbf{\C} & \D}
\end{tabular}
%\end{document}
\begin{filecontents*}{mydata2.csv}
  A;B;C;D
  a1;b1;c1;d1
  a2; "b21 and b22";c2;d2
\end{filecontents*}

\shorthandoff{;}
\begin{tabular}{c|c|c|c}
  \csvreader[separator=semicolon, head to column names, late after line=\\]%
  {mydata2.csv}{}%
  {\texttt{\A} & \B & \textbf{\C} & \D}
\end{tabular}
\shorthandon{;}
\end{document}

相关内容