当我尝试使用tabu
包从datatool
包中收集的数据渲染表格时,我遇到了这个奇怪的问题。这是一个工作示例:
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[verbose=true]{datatool}
\usepackage{datatool-base}
\usepackage{tabu}
\usepackage{ifthen}
\usepackage{longtable}
\begin{document}
\DTLsetseparator{|}
\DTLloaddb[]{isa}{isa.csv}
\begin{table}
\begin{center}
\begin{tabu} to \textwidth {|ll|X[l]|}
\hline
\multicolumn{2}{|c|}{Mnemonic} & Description %\\
\DTLforeach*[\DTLiseq{\family}{Arithmetic} \and \DTLiseq{\subfamily}{0}]{isa}{
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily} % Assign list
{ %
\DTLiffirstrow{\\ \hline \hline}{\\} %
\texttt{\mnemonic} & \texttt{\args} & \description
} % End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic} \and \DTLiseq{\subfamily}{1}]{isa}{
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily} % Assign list
{ %
\DTLiffirstrow{\\ \hline}{\\} %
\texttt{\mnemonic} & \texttt{\args} & \description
} % End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic} \and \DTLiseq{\subfamily}{2}]{isa}{
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily} % Assign list
{ %
\DTLiffirstrow{\\ \hline}{\\} %
\texttt{\mnemonic} & \texttt{\args} & \description
} % End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic} \and \DTLiseq{\subfamily}{3}]{isa}{
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily} % Assign list
{ %
\DTLiffirstrow{\\ \hline}{\\} %
\texttt{\mnemonic} & \texttt{\args} & \description
} % End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic} \and \DTLiseq{\subfamily}{4}]{isa}{
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily} % Assign list
{ %
\DTLiffirstrow{\\ \hline}{\\} %
\texttt{\mnemonic} & \texttt{\args} & \description
} % End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic} \and \DTLiseq{\subfamily}{5}]{isa}{
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily} % Assign list
{ %
\DTLiffirstrow{\\ \hline}{\\} %
\texttt{\mnemonic} & \texttt{\args} & \description
} % End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic} \and \DTLiseq{\subfamily}{6}]{isa}{
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily} % Assign list
{ %
\DTLiffirstrow{\\ \hline}{\\} %
\texttt{\mnemonic} & \texttt{\args} & \description
} % End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic} \and \DTLiseq{\subfamily}{7}]{isa}{
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily} % Assign list
{ %
\DTLiffirstrow{\\ \hline}{\\} %
\texttt{\mnemonic} & \texttt{\args} & \description
} % End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic} \and \DTLiseq{\subfamily}{8}]{isa}{
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily} % Assign list
{ %
\DTLiffirstrow{\\ \hline}{\\} %
\texttt{\mnemonic} & \texttt{\args} & \description
} % End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic} \and \DTLiseq{\subfamily}{9}]{isa}{
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily} % Assign list
{ %
\DTLiffirstrow{\\ \hline}{\\} %
\texttt{\mnemonic} & \texttt{\args} & \description
} % End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic} \and \DTLiseq{\subfamily}{10}]{isa}{
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily} % Assign list
{ %
\DTLiffirstrow{\\ \hline}{\\} %
\texttt{\mnemonic} & \texttt{\args} & \description
} % End loop
\\ \hline
\end{tabu}
\caption{Arithmetic instructions}
\label{tbl:a_instructions}
\end{center}
\end{table}
\end{document}
以下是使用的 CSV 文件:
"mnemonic"|"args"|"shortdescription"|"family"|"subfamily"
"addi.w"|"rdst,rsrc,IMM"|"Add immediate"|"Arithmetic"|"0"
"addui.w"|"rdst,rsrc,IMM"|"Add unsigned immediate"|"Arithmetic"|"0"
"subi.w"|"rdst,rsrc,IMM"|"Substract immediate"|"Arithmetic"|"1"
"subui.w"|"rdst,rsrc,IMM"|"Substract unsigned immediate"|"Arithmetic"|"1"
"add.w"|"rdst,rsrc1,rsrc2"|"Add GPR's"|"Arithmetic"|"2"
"add.l"|"rdst,rsrc1,rsrc2"|"Add long GPR's"|"Arithmetic"|"2"
"add.f"|"fdst,fsrc1,fsrc2"|"Add FPR's"|"Arithmetic"|"2"
"add.d"|"fdst,fsrc1,fsrc2"|"Add double FPR's"|"Arithmetic"|"2"
"sub.w"|"rdst,rsrc1,rsrc2"|"Substract GPR's"|"Arithmetic"|"3"
"sub.l"|"rdst,rsrc1,rsrc2"|"Substract long GPR's"|"Arithmetic"|"3"
"sub.f"|"fdst,fsrc1,fsrc2"|"Substract FPR's"|"Arithmetic"|"3"
"sub.d"|"fdst,fsrc1,fsrc2"|"Substract double FPR's"|"Arithmetic"|"3"
"mult.w"|"rdst,rsrc1,rsrc2"|"Multiply GPR's"|"Arithmetic"|"4"
"mult.l"|"rdst,rsrc1,rsrc2"|"Multiply GPR's into long GPR"|"Arithmetic"|"4"
"mult.f"|"fdst,fsrc1,fsrc2"|"Multiply FPR's"|"Arithmetic"|"4"
"mult.d"|"fdst,fsrc1,fsrc2"|"Multiply double FPR's"|"Arithmetic"|"4"
"div.w"|"rdst,rsrc1,rsrc2"|"Divide GPR's"|"Arithmetic"|"5"
"div.l"|"rdst,rsrc1,rsrc2"|"Divide long GPR's"|"Arithmetic"|"5"
"div.f"|"fdst,fsrc1,fsrc2"|"Divide FPR's"|"Arithmetic"|"5"
"div.d"|"fdst,fsrc1,fsrc2"|"Divide double FPR's"|"Arithmetic"|"5"
"rem.w"|"rdst,rsrc1,rsrc2"|"Remainder of GPR's"|"Arithmetic"|"6"
"rem.l"|"rdst,rsrc1,rsrc2"|"Remainder of long GPR's"|"Arithmetic"|"6"
"rl.w"|"rdst,rsrc1,rsrc2"|"Rotate left GPR"|"Arithmetic"|"7"
"rl.l"|"rdst,rsrc1,rsrc2"|"Rotate left long GPR"|"Arithmetic"|"7"
"rr.w"|"rdst,rsrc1,rsrc2"|"Rotate right GPR"|"Arithmetic"|"8"
"rr.l"|"rdst,rsrc1,rsrc2"|"Rotate right long GPR"|"Arithmetic"|"8"
"sl.w"|"rdst,rsrc1,rsrc2"|"Shift left GPR"|"Arithmetic"|"9"
"sl.l"|"rdst,rsrc1,rsrc2"|"Shift left long GPR"|"Arithmetic"|"9"
"sra.w"|"rdst,rsrc1,rsrc2"|"Shift right arithmetical GPR"|"Arithmetic"|"10"
"sra.l"|"rdst,rsrc1,rsrc2"|"Shift right arithmetical long GPR"|"Arithmetic"|"10"
注意看有些行下面有多余的空格。为什么会产生这些空格呢?
答案1
问题似乎是由代码中的虚假空格引起的。删除所有不需要的空格即可解决此问题:
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[verbose=true]{datatool}
\usepackage{datatool-base}
\usepackage{tabu}
\usepackage{ifthen}
\usepackage{longtable}
\begin{document}
\DTLsetseparator{|}
\DTLloaddb[]{isa}{isa.csv}
\begin{table}
\begin{center}
\begin{tabu} to \textwidth {|ll|X[l]|}
\hline
\multicolumn{2}{|c|}{Mnemonic} & Description%
\DTLforeach*[\DTLiseq{\family}{Arithmetic}\and\DTLiseq{\subfamily}{0}]{isa}{
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily}% Assign list
{%
\DTLiffirstrow{\\\hline\hline}{\\}%
\texttt{\mnemonic} & \texttt{\args} & \description
}% End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic}\and\DTLiseq{\subfamily}{1}]{isa}{%
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily}% Assign list
{%
\DTLiffirstrow{\\\hline}{\\}%
\texttt{\mnemonic} & \texttt{\args} & \description
}% End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic}\and\DTLiseq{\subfamily}{2}]{isa}{%
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily}% Assign list
{%
\DTLiffirstrow{\\\hline}{\\}%
\texttt{\mnemonic} & \texttt{\args} & \description
}% End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic}\and\DTLiseq{\subfamily}{3}]{isa}{%
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily}% Assign list
{%
\DTLiffirstrow{\\\hline}{\\}%
\texttt{\mnemonic} & \texttt{\args} & \description
}% End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic}\and\DTLiseq{\subfamily}{4}]{isa}{%
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily}% Assign list
{%
\DTLiffirstrow{\\\hline}{\\}%
\texttt{\mnemonic} & \texttt{\args} & \description
}% End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic}\and\DTLiseq{\subfamily}{5}]{isa}{%
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily}% Assign list
{%
\DTLiffirstrow{\\\hline}{\\}%
\texttt{\mnemonic} & \texttt{\args} & \description
}% End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic}\and\DTLiseq{\subfamily}{6}]{isa}{%
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily}% Assign list
{%
\DTLiffirstrow{\\\hline}{\\}%
\texttt{\mnemonic} & \texttt{\args} & \description
}% End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic}\and\DTLiseq{\subfamily}{7}]{isa}{%
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily}% Assign list
{%
\DTLiffirstrow{\\\hline}{\\}%
\texttt{\mnemonic} & \texttt{\args} & \description
}% End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic}\and\DTLiseq{\subfamily}{8}]{isa}{%
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily}% Assign list
{%
\DTLiffirstrow{\\\hline}{\\}%
\texttt{\mnemonic} & \texttt{\args} & \description
}% End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic}\and\DTLiseq{\subfamily}{9}]{isa}{%
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily} % Assign list
{%
\DTLiffirstrow{\\\hline}{\\}%
\texttt{\mnemonic} & \texttt{\args} & \description
}% End loop
\DTLforeach*[\DTLiseq{\family}{Arithmetic}\and\DTLiseq{\subfamily}{10}]{isa}{%
\mnemonic=mnemonic,
\args=args,
\description=shortdescription,
\family=family,
\subfamily=subfamily}% Assign list
{%
\DTLiffirstrow{\\\hline}{\\}%
\texttt{\mnemonic} & \texttt{\args} & \description
}% End loop
\\\hline
\end{tabu}
\caption{Arithmetic instructions}
\label{tbl:a_instructions}
\end{center}
\end{table}
\end{document}
结果: