Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

A database in a format that can be freely defined had entities with the following attributes:

ID, HEXNUMBER, LATEXFORMULA, LITERAL.

  • The ID is unique and can contain numbers, dots and dashed. Example: MystructOne.val16_A

  • HEXNUMBER is always an address like 0xD0007246.

  • LATEXFORMULA will look like so: $p = \frac{57426}{10^8} \mathrm{[V]} \cdot i$

  • The LITERAL will contain type information: UINT(t16)

I am free to save the data however I want, but the contents is fixed. How can I save them so that I can access them in LaTeX with something like the following:

\custom{MystructOne.val16_A}{ID}
\custom{MystructOne.val16_A}{HEXNUMBER}
\custom{MystructOne.val16_A}{LATEXFORMULA}
\custom{MystructOne.val16_A}{LITERAL}

and at one point I would like to generate a table using something like the following:

foreach key in keysource
{
    \custom{key}{ID}
    \custom{key}{HEXNUMBER}
    \custom{key}{LATEXFORMULA}
    \custom{key}{LITERAL}
}

What can I do in LaTeX to access data like that?

EDIT #2 (replaced previous edit)

This is how far i have gotten now:

\documentclass{article}
\usepackage{hyperref}

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

\usepackage[T1]{fontenc}

\usepackage{csvtools}
\setcsvseparator{;}
\def\verbID{\texttt{\edef\tmp{\insertID}\expandafter\detokenize\expandafter{\tmp}}}
\newcommand{\DATARow}[4]{\keylabel{#1}{#1} & #2 & #3 & #4\\[.5ex]}
%The keylabel throws a lot of errors here

\begin{document}

%%% - Creating the Table
\CSVtotabular{stackoverflow.csv}%
  {|c|c|c|c|}%
  {\DATARow{ID}{REGISTER}{TYPE}{FORMULA}}%
  {\DATARow{\verbID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}%
  {\DATARow{\verbID}{\insertREGISTER}{\insertTYPE}{\insertFORMULA}}

\newpage  

%%% - Referencing something in the Table:
\noindent
%\ref{A_B_C_D}\\  - Does not work at all
%\ref{ONE}\\  - Does not work at all
%\ref{TWO.FOUR}\\  - Does not work at all
%\ref{Thirty[2]}\\  - Does not work at all
%\ref{TreeHouse}\\  - Does not work at all

\end{document}

The final step will be to be able to set Keylables and reference them. I want the referencing to work like in this answer here.

But i am getting errors and warnings.

  • I can not set the keys, some envireonment collision i dont understand.
  • I was not able to test this, but i think i need a \verbRef.
  • I need to supress the warnings in texlipse that will occur since the keys can only be set during compile time. I dont know how to do that.

These final items are the ones in need to clear before i can reasonably say that i am looking up the values that i need.

Here is a demo csv file that can be used with this problem:

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$
share|improve this question
7  
The ideal tool for this would be datatool, provided that you have the data available as CSV. –  Werner Feb 22 '13 at 16:20
add comment

3 Answers 3

up vote 6 down vote accepted
+50

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

share|improve this answer
    
This seems to work akkording to the screenshot, and for the most part it also works for me. However the 'Reference entries in the database' part does not work... i think texlipse is the problem here - how do i get whis to work with ecplise? My erroe reads: Description Resource Path Location Type Undefined control sequence: \useentry{ONE}{REGISTER} (followed by: ) demo.tex /MiniDemo line 58 Texlipse Build Error –  Johannes Mar 4 '13 at 16:39
1  
@Johannes You may need to update your version of datatool (the current version is 2.13) –  Nicola Talbot Mar 4 '13 at 18:02
add comment

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

share|improve this answer
    
I love it :) One thing that i can not see is how i would hyperreference the generated list later in the project. That keylabel part of the mini example. –  Johannes Feb 27 '13 at 13:13
    
@Johannes I don't understand what kind of hyperlinks you have in mind. –  egreg Feb 27 '13 at 13:15
    
This kind: tex.stackexchange.com/questions/99472/… In particular the table with values will be a reference. throughout the document the writer has to do something like \ref{A_B_C_D} so the reader can click in the document if he/she needs the table information. –  Johannes Feb 27 '13 at 13:36
add comment

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.

share|improve this answer
    
I do not understand this, nor do i know how to compile it. –  Johannes Feb 27 '13 at 9:05
    
@Johannes: If this approach might work for you, I can fill out the details. If you can program Python, Lua is not difficult to learn. Apologies for the first edition of this answer: a typo caused an unfinished answer to be submitted. –  Charles Stewart Feb 27 '13 at 9:08
    
@johannes: Could you provide a small dataset? I ask because the above is just guessing at details about how you are representing the fields –  Charles Stewart Feb 27 '13 at 9:10
    
i am a programmer and i do know lua. i scripted for WOW for a while. I do not have the toolchain to use Lua in a LaTeX context though. What i dont understand is how lua is connected to latex - i thought latex was based in pascal. I dont think this is a good idea since the endproduct will be release to colleagues who may be challenges even by texlipse. Regarding the dataset, at the bottom of the questionpost is a copy pasteable version of a csv file... i edited it in recently you may have to reload the page to see it. –  Johannes Feb 27 '13 at 9:19
1  
@Johannes: The modern Tex engines are based on the C code generated by the Web2C team. The most widely used engine is Pdftex; Luatex is an extension of Pdftex that allows Lua to be combined with Tex: all the Lua code you need to run can be embedded within the document, but you then have to use lualatex (and not pdflatex or xelatex) to execute. –  Charles Stewart Feb 27 '13 at 9:25
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.