如果我尝试编译以下 LaTeX 文件:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{datatool}
\begin{document}
\tableofcontents
\appendix
\section{Bill of Materials}
\DTLloadrawdb{bom}{test.csv}
\DTLdisplaydb{bom}
\end{document}
以下内容为test.csv
:
"name","quantity"
"10μF 16V",5
"470Ω 1/2W",10
"33k×9",1
我收到此错误:
! Undefined control sequence.
\GenericError ...
#4 \errhelp \@err@ ...
l.9 \DTLloadrawdb{bom}{test.csv}
我该如何修复/解决这个问题?
答案1
我发现对于 UTF-8 输入 CSV 文件,使用 UTF-8 感知引擎(LuaLaTeX 或 XeLaTeX)要简单得多。无论哪种方式,datatool
如果您的字体没有这些字符,您还需要告诉如何映射这些字符。
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.csv}
"name","quantity"
"10μF 16V",5
"470Ω 1/2W",10
"33k×9",1
\end{filecontents*}
\usepackage{fontspec}
\usepackage{datatool}
\DTLrawmap{μ}{$\mu$} % add these if your chosen font doesn’t have the character
\DTLrawmap{Ω}{$\Omega$}
\DTLrawmap{×}{$\times$}
\begin{document}
\tableofcontents
\appendix
\section{Bill of Materials}
\DTLloadrawdb{bom}{\jobname.csv}
\DTLdisplaydb{bom}
\end{document}
答案2
您可以在 datatool 使用的 edef 中确保 utf8 字符的安全
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{datatool}
\usepackage[T1]{fontenc}
\DeclareUnicodeCharacter{03A9}{\ensuremath{\omega}}
\DeclareUnicodeCharacter{03BC}{\ensuremath{\mu}}
\DeclareUnicodeCharacter{00D7}{\ensuremath{\times}}
\begin{document}
\tableofcontents
\appendix
\section{Bill of Materials}
\makeatletter
\let\zz\UTFviii@two@octets
\def\UTFviii@two@octets#1#2{\noexpand\UTFviii@two@octets\noexpand#1\string#2}
\DTLloadrawdb{bom}{test.csv}
\let\UTFviii@two@octets\zz
\DTLdisplaydb{bom}
\end{document}