I have a generated database and need to look up values

This seems to do what you want, at least for what I can understand.

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

\setcsvseparator{;}

\def\IDs{}
\let\IDdo\relax
\applyCSVfile{johancsv.csv}
 {%
  \expandafter\xdef\csname joh@\insertID@@@ID\endcsname{\detokenize\expandafter{\insertID}}%
  \expandafter\gdef\csname joh@\insertID@@@REGISTER\expandafter\endcsname\expandafter{\insertREGISTER}%
  \expandafter\gdef\csname joh@\insertID@@@TYPE\expandafter\endcsname\expandafter{\insertTYPE}%
  \expandafter\gdef\csname joh@\insertID@@@FORMULA\expandafter\endcsname\expandafter{\insertFORMULA}%
  \xdef\IDs{\IDs\IDdo{\insertID}}
 }

\newcommand{\use}[2]{\csname joh@#1@@@#2\endcsname}
\newcommand{\eachID}[1]{\def\IDdo##1{#1}\IDs}

\begin{document}

\use{A_B_C_D}{FORMULA}

\bigskip

\use{TreeHouse}{TYPE}

\bigskip

\newcounter{formulas}
\eachID{%
  \stepcounter{formulas}\noindent
  \theformulas\quad\texttt{\use{#1}{ID}}
  \use{#1}{FORMULA}\par\medskip
}

\end{document}

For each line of the CSV file, we associate to each ID some macros that can later be accessed with

\use{<ID>}{<FIELD>}

You can even say

\use{A_B_C_D}{ID}

for printing the ID without worrying about underscores (provided the T1 encoding is chosen); an example is in the \eachID at the end.

The macro \eachID cycles through the list of ID's and performs the actions specified in the argument, where #1 stands for the current ID.

enter image description here


I'd do this in LuaTex, because Lua can do this kind of custom record extraction more simply than Tex can.

The Lua code below is quite simple (I'll test it if you provide an example data set):

t={} -- The table we store the result in
for line in io.lines "filename" do
  id, hex, lstr, lit = line:match "^([^,]+),%s*(0x%x+),%s*(.*),%s*[^,]+$"
  t[1+#t] = {id=id, hex=hex, lstr=lst, lit=lit}
end

and fills the table t with named fields for the four comma-separated field in the input file; the code checks that the hex field actually takes the form of a hex number. It forbids commas in any field but the Latex code field.

The above Lua can be embedded in a Lua Latex document using \directlua of one of the Lua wrappers, and your \custom macro can also be defined to access table records using \directlua.


Don't use csvtools it's obsolete. Here's an answer that uses the replacement package datatool:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{datatool}
\usepackage{hyperref}

\begin{filecontents}{test.csv}
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$
\end{filecontents}

\DTLsetseparator{;}
\DTLloaddb{data}{test.csv}

% syntax: \useentry{ID value}{field}
\newcommand{\useentry}[2]{%
 % fetch row for given ID field:
 \dtlgetrowforvalue{data}{\dtlcolumnindex{data}{ID}}{#1}%
 % access required field from current row:
 \dtlgetentryfromcurrentrow{\thisval}{\dtlcolumnindex{data}{#2}}%
 \thisval
}

\newcounter{keylabel}
\newcommand*{\keylabel}[2]{%
    \leavevmode%
    \raisebox{2ex}[0pt][0pt]{%
    \renewcommand*{\thekeylabel}{#1}%
    \refstepcounter{keylabel}%
    \label{#2}%
    }%
    #1%
}

\makeatletter
\DeclareRobustCommand*{\dispID}[1]{%
  \def\thisID{#1}%
  \@onelevel@sanitize\thisID
  \texttt{\thisID}%
}
\makeatother

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


\begin{document}

Reference entries in the database:

\useentry{A_B_C_D}{FORMULA}

\useentry{Thirty[2]}{REGISTER}

Display all entries in a table:

\begin{tabular}{llll}
ID & Register & Type & Formula\\
\DTLforeach*{data}{\ID=ID,\Register=REGISTER,\Type=TYPE,\Formula=FORMULA}%
{%
  \expandafter\DATARow\expandafter{\ID}{\Register}{\Type}{\Formula}%
}
\end{tabular}

Reference something in the table:

\ref{A_B_C_D}
\ref{ONE}
\ref{TWO.FOUR}
\ref{Thirty[2]}
\end{document}

This produces:

Resulting document

Tags:

Database