如何将从(CSV)文件读取的字符串转换为计数器

如何将从(CSV)文件读取的字符串转换为计数器

我有一个带有标签和(页面)nrs 的 csv 文件,如下所示:

tag,nr
t1,1
t2,5
t3,9

nr-column 指的是 pdf 文件中的页码。每页都有对应的标签。给定一个标签,我想在我的 latex 文档中的图中显示相应的页面。

使用 datatools,我可以加载 csv 文件并获取给定标签的页码。例如,对于标签,t2我获取页码5

但是现在,当我将该页面编号作为page=...参数提供给 includegraphics 时,我收到了错误消息

“!缺失数字,视为零。”`

我应该采取什么额外步骤来将乳胶5字符串转换为 includegraphics 可以处理的页面编号?

这是一个触发问题的简化版本(使用计数器而不是includegraphics):

\documentclass{article}
\usepackage{datatool}

\begin{document}

\DTLloaddb{tags}{prestags.csv}
\newcommand{\pagenr}{\DTLfetch{tags}{tag}{t2}{nr}}

\newcounter{snr}
\addtocounter{snr}{\pagenr}

\end{document}

% Output:

! Missing number, treated as zero.
<to be read again> 
                   \let 
l.10 \addtocounter{snr}{\pagenr}

我应该怎样修复此问题?

答案1

问题是\DTLfetch宏不可扩展。您可以使用此处描述的技术:

来克服这个问题。下面是一个实际的例子:

\documentclass{article}
\begin{filecontents}{\jobname.csv}
tag,nr
t1,1
t2,5
t3,9
\end{filecontents}
\usepackage{datatool}
\newcommand{\getvar}[1]{\DTLfetchsave{\myvar}{tags}{tag}{#1}{nr}}
\newcommand{\DTLfetchsave}[5]{%
  \edtlgetrowforvalue{#2}{\dtlcolumnindex{#2}{#3}}{#4}%
  \dtlgetentryfromcurrentrow{\dtlcurrentvalue}{\dtlcolumnindex{#2}{#5}}%
  \let#1\dtlcurrentvalue
}
\newcounter{snr}
\begin{document}

\DTLloaddb{tags}{prestags.csv}

\setcounter{snr}{3}
The counter value is \thesnr

\getvar{t2}
\addtocounter{snr}{\myvar}
The counter value is: \thesnr

\end{document}

代码输出

答案2

这里,我使用readarray包来消化 csv 文件,并listofitems使用 read 将数据放入列表中。宏\getvar是可扩展的。

\begin{filecontents*}[overwrite]{prestags.csv}
tag,nr
t1,1
t2,5
t3,9
\end{filecontents*}
\documentclass{article}
\usepackage{readarray,graphicx}
\begin{document}
\readarraysepchar{\\}
\readdef{prestags.csv}\mycsvdata
\setsepchar{\\/,}
\readlist*{\mycsvlist}{\mycsvdata}
\foreachitem\z\in\mycsvlist[]{%
  \expandafter\gdef\csname mytag\mycsvlist[\zcnt,1]\expandafter
    \endcsname\expandafter{\expandafter
    \mycsvlist\expandafter[\zcnt,2]}%
}
\newcommand\getvar[1]{\csname mytag#1\endcsname}

The t2 tag corresonds with page \getvar{t2}

\includegraphics[page=\getvar{t3},scale=.6]{readarray.pdf}
\end{document}

在此处输入图片描述

答案3

如果您不介意全局定义一个临时宏,那么您可以使用\DTLassignfirstmatch

编译以下示例时,您会收到两条错误消息。

这是故意的,这样您就可以看到在尝试检索不存在的值时会发生什么。

\begin{filecontents*}{prestags.csv}
tag,nr
t1,1
t2,5
t3,9
\end{filecontents*}

\makeatletter
\newcommand\MyStuffDTLfetchsavePackageError[7]{%
  \PackageError{#1}{#7}{%
    In order to retrieve from database `#2`\MessageBreak
    the value of the key `#5` in the first row\MessageBreak
    where the key `#3` has value `#4`,\MessageBreak
    database `#2` must exist and have keys\MessageBreak
    `#3` and `#5` and a row where the key `#3`\MessageBreak
    has the value `#4`.%
  }%
  \global\let#6=\dtlnovalue
}%
\DeclareRobustCommand{\MyStuffDTLfetchsave}[5]{%
  %#1 = macro to store as result
  %#2 = data base name
  %#3 = key 1
  %#4 = value of key 1
  %#5 = key 2
  \@bsphack
  \DTLifdbexists{#2}{%
    \DTLifhaskey{#2}{#3}{%
      \DTLifhaskey{#2}{#5}{%
        \global\let#1=\dtlnovalue
        \DTLassignfirstmatch{#2}{#3}{#4}{#1=#5}%
        \ifx#1\dtlnovalue\expandafter\@firstofone\else\expandafter\@gobble\fi
        {%
          \MyStuffDTLfetchsavePackageError{My Stuff}{#2}{#3}{#4}{#5}{#1}%
             {In data base `#2' no row's key `#3' holds the value `#4'}%
        }%
      }{%
        \MyStuffDTLfetchsavePackageError{My Stuff}{#2}{#3}{#4}{#5}{#1}%
          {There is no key `#5' in data base `#2'}%
      }%
    }{%
      \MyStuffDTLfetchsavePackageError{My Stuff}{#2}{#3}{#4}{#5}{#1}%
        {There is no key `#3' in data base `#2'}%
    }%
  }{%
    \MyStuffDTLfetchsavePackageError{My Stuff}{#2}{#3}{#4}{#5}{#1}%
      {Data base `#2' doesn't exist}%
  }%
  \@esphack
}%
\makeatother

\documentclass{article}
\usepackage{datatool}

\newcounter{snr}

\begin{document}

\DTLloaddb{tags}{prestags.csv}

\setcounter{snr}{3}%
\noindent The counter value is: \thesnr

\MyStuffDTLfetchsave{\scratchy}{tags}{tag}{t1}{nr}%
\ifx\scratchy\dtlnovalue
  \noindent The value of the key `nr` in the row where the key `tag` has the value `t1` could not be retrieved.

  \noindent Calculations where this value would have been used were not performed.
\else
  \noindent The value of the key `nr` in the row where the key `tag` has the value `t1` is: \scratchy

  \addtocounter{snr}{\scratchy}%
  \noindent After adding that value to the counter, the counter value is: \thesnr
\fi

\MyStuffDTLfetchsave{\scratchy}{tags}{tag}{t2}{nr}%
\ifx\scratchy\dtlnovalue
  \noindent The value of the key `nr` in the row where the key `tag` has the value `t2` could not be retrieved.

  \noindent Calculations where this value would have been used were not performed.
\else
  \noindent The value of the key `nr` in the row where the key `tag` has the value `t2` is: \scratchy

  \addtocounter{snr}{\scratchy}%
  \noindent After adding that value to the counter, the counter value is: \thesnr
\fi

\MyStuffDTLfetchsave{\scratchy}{tags}{tag}{t3}{nr}%
\ifx\scratchy\dtlnovalue
  \noindent The value of the key `nr` in the row where the key `tag` has the value `t3` could not be retrieved.

  \noindent Calculations where this value would have been used were not performed.
\else
  \noindent The value of the key `nr` in the row where the key `tag` has the value `t3` is: \scratchy

  \addtocounter{snr}{\scratchy}%
  \noindent After adding that value to the counter, the counter value is: \thesnr
\fi

\MyStuffDTLfetchsave{\scratchy}{tags}{tag}{t4}{nr}%
\ifx\scratchy\dtlnovalue
  \noindent The value of the key `nr` in the row where the key `tag` has the value `t4` could not be retrieved.

  \noindent Calculations where this value would have been used were not performed.
\else
  \noindent The value of the key `nr` in the row where the key `tag` has the value `t4` is: \scratchy

  \addtocounter{snr}{\scratchy}%
  \noindent After adding that value to the counter, the counter value is: \thesnr
\fi

\end{document}

在此处输入图片描述

答案4

根据建议@cabohahDTLfetch问题中的那行可以用下面两行替换:

\DTLgetlocation{\rownr}{\colnr}{tags}{t2}
\DTLgetvalue{\pagenr}{tags}{\rownr}{\dtlcolumnindex{tags}{nr}}

由于这是一个简单的改变,所以我最终使用了它。

相关内容