# How can I create an index or TOC using latexdb and mysql?

I have a database full of names, addresses, etc.. We use them at my work to make sure only people in the database are allowed inside (or denied entry, if they've ever done something wrong). I created a document with pdflatexdb, and by using longtable, I now have a 'working' document that I can live with if I have to.

However, as we add people to the database everyday, the "list" gets larger and less manageable as far a scanning for a name or id number and the like. What I'd like to figure out is how to create an "index" that will show which pages last names (a field in the DB) start with... IE - names that start with A are on page one, B page 50, C page 60, and so forth. Here is a working example:

\documentclass[10pt,twoside]{report}

\usepackage{longtable}
\usepackage[left=.5in, right=.5in, top=1cm, bottom=.8in]{geometry}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyfoot{}
\fancyfoot[RE, RO]{\thepage}
\fancyfoot[C]{Generated: \today}

\texdbconnection{CONNECTION STUFF}

%% BEGIN TeXDBdef

%% EHD TeXDBdef

\begin{document}
\begin{center}\section*{These people are allowed inside the building.}\end{center}
\begin{center}\Large{Ordered by name}\end{center}
\begin{longtable}{@{\extracolsep{\fill}}llp{8cm}l}
\vspace{.8em}
\end{longtable}

\end{document}


I am a relative n00b to LaTeX, so please be gentle if my document has glaring errors (I'm assuming some of my code is redundant or even useless).

Thanks.

-
FYI, I'm working on a PHP frontend for this, so please don't comment on the uselessness of a paper list... –  jmd9qs Apr 13 '13 at 11:29

It's not possible to run the code without the connection package and something to connect to however

\Large{Ordered by name}


size commands like \Large don't take an argument so if you use in that form the {} are not delimiting an argument but making a group which is harmless in this case but confusing, better to leave out the braces.

But I would guess that you can replace

\texttt{##lname,}


by

\labelname{##lname}\texttt{##lname,}


where \labelname gets the first letter and does a \label if it is new, something like

\def\labelname#1{\xlabelname#1.\relax}
\def\xlabelname#1#2\relax{%
\def\tmp{#1}%
\ifx\tmp\firstletter
\else
\global\let\firstletter\tmp
\label{letter-#1}%
\fi}


then at the end you just need to use \pageref on each letter in turn:

\documentclass{article}

\def\labelname#1{\xlabelname#1.\relax}
\def\xlabelname#1#2\relax{%
\def\tmp{#1}%
\ifx\tmp\firstletter
\else
\global\let\firstletter\tmp
\label{letter-#1}%
\fi}

\begin{document}

\labelname{Alan}Alan
\labelname{Brian}Brian
\labelname{David}David

\newpage

\labelname{Duncan}Duncan
\labelname{Fred}Fred

\section{Name index}

\makeatletter
\def\refnameletter#1{%
\expandafter\ifx\csname r@letter-#1\endcsname\relax\else
\par\noindent#1: \pageref{letter-#1}\par
\fi
\if Z#1\else\expandafter\refnameletter\fi}

\refnameletter ABCDEFGHIJKLMNOPQRSTUVWXYZ

\end{document}


-
thanks! I know this isn't testable on your end, but I'll give it a go and get back to you later on today. –  jmd9qs Apr 13 '13 at 12:16
I should probably comment for the record that there are letters other than A-Z and this will silently ignore them, but it saved worrying about encodings and stuff. –  David Carlisle Apr 13 '13 at 12:18
I figured that, but it suits my needs as I don't need to worry about special characters or anything... Thanks again! –  jmd9qs Apr 13 '13 at 13:02
Thanks for your solution David! As of now it's not gonna work for me, I think I'm going to have to give in and write a perl script... probably a cleaner solution, and more maintainable. –  jmd9qs Apr 13 '13 at 16:10