如何將從(CSV)檔案讀取的字串轉換為計數器

如何將從(CSV)檔案讀取的字串轉換為計數器

我有一個帶有標籤和(頁面)nrs 的 csv 文件,如下所示:

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

nr 欄位是指 pdf 檔案中的頁碼。每個頁面都有對應的標籤。給定一個標籤,我想在我的乳膠文件中的圖中顯示相應的頁面。

使用 datatools,我可以載入 csv 文件,並取得給定標籤的頁面編號。例如,對於標籤t2我得到 page nr 5

但是現在,當我將該頁號作為page=...includegraphics 的參數提供時,我收到錯誤訊息

「!缺少數字,視為零。」`

我應該採取什麼額外步驟將乳膠5字串轉換為 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讀取以將資料放入列表中。巨集\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

正如建議的@卡博哈DTLfetch問題中的行可以替換為以下兩行:

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

由於這是一個簡單的更改,這就是我最終使用的。

相關內容