Skip to content

Instantly share code, notes, and snippets.

@fmitha
Created May 24, 2018 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmitha/034d82e467b348653e05fc9f0fbe388e to your computer and use it in GitHub Desktop.
Save fmitha/034d82e467b348653e05fc9f0fbe388e to your computer and use it in GitHub Desktop.
\begin{filecontents*}{\jobname.dat}
Hammer001, Hammer, 1 , 0 , 1 , 10 , 1 , light (add some words here to wrap around)
Hammer002, Hammer, 2 , 0 , 1 , 10 , 1 , heavy
Hammer003, Hammer, 3 , 0 , 1 , 10 , 1 , really heavy
Longsword001,Longsword, 1 , -1 , 2 , 75 , 2 , one-handed
Longsword002,Longsword, 2 , -1 , 2 , 75 , 2 , two-handed
Longsword003,Longsword, 3 , -1 , 2 , 75 , 2 , three-handed
\end{filecontents*}
\documentclass{article}
\usepackage{array}
\usepackage{datatool}
\usepackage{longtable}
\usepackage{etoolbox}
\makeatletter
\let\oldref\ref
\def\ref#1{%
\expandafter\ifx\csname R@#1\endcsname\relax
\global\expandafter\let\csname R@#1\endcsname\@empty
\immediate\write\@auxout{%
\string\gappto\string\ReferencedIDs{#1,}%
}%
\fi
\oldref{#1}%
}
\def\ReferencedIDs{}
\makeatother
\newcommand{\colhead}[1]{\multicolumn{1}{>{\bfseries}l}{#1}}
\newcounter{tabenum}\setcounter{tabenum}{0}
\newcommand{\nextnuml}[1]{%
\refstepcounter{tabenum}\thetabenum.\label{#1}%
}
\newcommand{\PrintDTLTable}[2][]{%
% #1 = list of rowIDs
% #2 = database to search
\begin{longtable}{l l l l l l l m{2in}}
& \colhead{Label} & \colhead{Cost} & \colhead{Weight} &
\colhead{PropA} & \colhead{PropB} & \colhead{PropC} & \colhead{Description}\\
\hline
\DTLforeach
[%
\(\equal{#1}{}\AND\DTLisSubString{\ReferencedIDs}{\RowID}\)
\OR
\(\DTLisSubString{#1}{\RowID}\AND\DTLisSubString{\ReferencedIDs}{\RowID}\)%
]
{#2}{%
\RowID=RowID,%
\Label=Label,%
\Cost=Cost,%
\Weight=Weight,%
\PropA=PropA,%
\PropB=PropB,%
\PropC=PropC,%
\Description=Description%
}{%
\nextnuml{\RowID} & \Label &\Cost & \Weight & \PropA & \PropB & \PropC & \Description \\
}%
\end{longtable}
}
\newcommand{\PrintTable}[2][]{%
% #1 = list of rowIDs
% #2 = database to search
\begin{longtable}{l l l l l l l m{2in}}
& \colhead{Label} & \colhead{Cost} & \colhead{Weight} &
\colhead{PropA} & \colhead{PropB} & \colhead{PropC} & \colhead{Description}\\
\hline
\DTLforeach
[%
\(\equal{#1}{}\AND\DTLisSubString{\ReferencedIDs}{\RowID}\)
\OR
\(\DTLisSubString{#1}{\RowID}\AND\DTLisSubString{\ReferencedIDs}{\RowID}\)%
]
{#2}{%
\RowID=RowID,%
\Label=Label,%
\Cost=Cost,%
\Weight=Weight,%
\PropA=PropA,%
\PropB=PropB,%
\PropC=PropC,%
\Description=Description%
}{%
\ref{\RowID} & \Label &\Cost & \Weight & \PropA & \PropB & \PropC & \Description \\
}%
\end{longtable}
}
\makeatletter
\let\oldref\ref
\def\ref#1{%
\immediate\write\@auxout{%
\string\gappto\string\ReferencedIDs{#1,}%
}%
\oldref{#1}%
}
\def\ReferencedIDs{}
\makeatother
\begin{document}
% \DTLsetseparator{&}% Define separator of the data
\DTLloaddb[noheader,keys={RowID,Label,Cost,Weight,PropA,PropB,PropC,Description}]{myDB}{\jobname.dat}
% \DTLdisplaydb{myDB}% Useful for debugging.
\PrintDTLTable[Hammer001,Hammer003,Longsword003]{myDB}
\PrintDTLTable[Hammer002,Longsword001,Longsword002,Longsword003]{myDB}
This is a reference to ~\ref{Hammer003}.
This is a reference to ~\ref{Longsword002}.
This is a reference to ~\ref{Longsword001}.
This is a reference to ~\ref{Longsword003}.
\PrintTable[Longsword001, Longsword002]{myDB}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment