逐字逐句地...一个包从一个文件中传递一个字符串,并且该字符串需要被打印出来而无需解释

逐字逐句地...一个包从一个文件中传递一个字符串,并且该字符串需要被打印出来而无需解释

csvtools 包在其文档中对可解释的输入发出警告,但据我所知没有提供解决方案。

此代码生成一个表

\documentclass{article}
\usepackage{csvtools}

\begin{document}

\newcommand{\DATARow}[4]{#1 & #2 & #3 & #4\\[.5ex]}

\setcsvseparator{;}

\CSVtotabular%
{stackoverflow.csv}%
{|c|c|c|c|}%
{\DATARow{ID}{REGISTER}{TYPE}{FORMULA}}%
{\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}%
{\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}

\end{document}

使用此文件作为源:

ID;REGISTER;TYPE;FORMULA
A_B_C_D;0xD000720C;UINT(16);$Y = \frac{57426}{10^8} \cdot X$
ONE;0xD020720C;UINT(8);$Y = 109 \cdot X$
TWO.FOUR;0xD080720C;INT(16);$Y = \frac{57426}{X}$
Thirty[2];0xD009720C;INT(8);$Y = X$
TreeHouse;0xD200720C;UINT(32);$Y = \frac{X}{10^8} \cdot X$

问题出在 ID 字符串中:A_B_C_D它应该按原样打印,但被解释为数学并打印如下$A_{BCD}$

DATARow我该如何修改才能A_B_C_D使其正确显示?

答案1

在此处输入图片描述

您可以使用它\detokenize来制作_安全的T1编码并\texttt以文字方式显示它。

\documentclass{article}
\usepackage{csvtools}
\usepackage[T1]{fontenc}
\begin{document}

\newcommand{\DATARow}[4]{#1 & #2 & #3 & #4\\[.5ex]}
\def\verbid{%
\texttt{\edef\tmp{\insertID}\expandafter\detokenize\expandafter{\tmp}}}

\setcsvseparator{;}

\CSVtotabular%
{stackoverflow.csv}%
{|c|c|c|c|}%
{\DATARow{ID}{REGISTER}{TYPE}{FORMULA}}%
{\DATARow{\verbid}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}%
{\DATARow{\verbid}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}

\end{document}

答案2

您有多种可能性。以下所有解决方案都允许_在具有通常含义的公式中使用。我更喜欢第三个。

使_活跃

\documentclass{article}
\usepackage{csvtools}

\newcommand{\DATARow}[4]{#1 & #2 & #3 & #4\\[.5ex]}
\setcsvseparator{;}

%%% Make _ active
\catcode`\_=\active
\protected\def_{\ifmmode\sb\else\_\fi}
%%%

\begin{document}

\CSVtotabular{stackoverflow.csv}%
  {|c|c|c|c|}%
  {\DATARow{ID}{REGISTER}{TYPE}{FORMULA}}%
  {\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}%
  {\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}

\end{document}

使_普通字符和数学在全球范围内活跃

请注意,此解决方案需要 T1 编码

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{csvtools}

\newcommand{\DATARow}[4]{#1 & #2 & #3 & #4\\[.5ex]}
\setcsvseparator{;}

%%% Make _ a normal character and math active
\catcode`\_=12
\AtBeginDocument{\mathcode`\_=\string"8000 }
\begingroup\lccode`\~=`\_
\lowercase\endgroup{\let~\sb}
%%%

\begin{document}

\CSVtotabular{stackoverflow.csv}%
  {|c|c|c|c|}%
  {\DATARow{ID}{REGISTER}{TYPE}{FORMULA}}%
  {\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}%
  {\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}

\end{document}

使_角色和数学仅在本地活跃

再次,T1 编码是必要的。

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{csvtools}

\newcommand{\DATARow}[4]{#1 & #2 & #3 & #4\\[.5ex]}
\setcsvseparator{;}

%%% Make _ a normal character and math active
\newcommand{\specialunderscore}{%
  \catcode`\_=12
  \mathcode`\_=\string"8000
  \begingroup\lccode`\~=`\_
  \lowercase\endgroup{\let~\sb}}

%%% Define a wrapper around \CSVtotabular
\newcommand{\xCSVtotabular}[6][]{%
  \begingroup#1\CSVtotabular{#2}{#3}{#4}{#5}{#6}\endgroup}

%%%

\begin{document}

\xCSVtotabular[\specialunderscore]{stackoverflow.csv}%
  {|c|c|c|c|}%
  {\DATARow{ID}{REGISTER}{TYPE}{FORMULA}}%
  {\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}%
  {\DATARow{\insertID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}

\end{document}

接受\xCSVtotabular一个可选参数,该参数应由诸如\specialunderscore或类似的分配组成,这些分配在本地改变某些标记的含义。

相关内容