As a follow up to Adding TikZ code to TOC, I have a TikZ code added to each index entry. That code may contain these specific letters: A, N, C, P and Q. See the following example:
\documentclass{article}
\usepackage[titles]{tocloft}
\usepackage{tikz}
\newcommand*\info[1]{%
\begin{tikzpicture}
\node[draw,inner sep=1pt, minimum height=0.32cm, minimum width=0.9cm] {\tt\scriptsize #1};
\end{tikzpicture}}
\newcommand{\listfoo}{List of foo}
\newlistof[section]{foo}{idf}{\listfoo}
\begin{document}
\refstepcounter{foo}
\addcontentsline{idf}{foo}{\protect \info{ANCP}\ Hello world.}
\refstepcounter{foo}
\addcontentsline{idf}{foo}{\protect \info{ANCPQ}\ Hello bar.}
\listoffoo
\end{document}

As you can see, (pardon my possible misuse of the terms) both text objects are centralized, but the latter - because of the letter Q - does not look that aesthetic to me.
Since I know all the possible letters I may use, and as the only problematic letter is the Q, I wrote this fix using the xstring package and Martin Scharrer's suggestion:
\documentclass{article}
\usepackage[titles]{tocloft}
\usepackage{tikz}
\usepackage{xstring}
\newcommand*\info[1]{%
\begin{tikzpicture}
\IfSubStr{#1}{Q}{%
\node[draw,inner sep=1pt, minimum height=0.32cm, minimum width=0.9cm] {\raisebox{\dimexpr-.5\height+.5\depth-.8ex\relax}{\tt\scriptsize #1}};}{%
\node[draw,inner sep=1pt, minimum height=0.32cm, minimum width=0.9cm] {\tt\scriptsize #1};}
\end{tikzpicture}}
\newcommand{\listfoo}{List of foo}
\newlistof[section]{foo}{idf}{\listfoo}
\begin{document}
\refstepcounter{foo}
\addcontentsline{idf}{foo}{\protect \info{ANCP}\ Hello world.}
\refstepcounter{foo}
\addcontentsline{idf}{foo}{\protect \info{ANCPQ}\ Hello bar.}
\listoffoo
\end{document}

That pretty much solves my issue.
I'd like to know if there are better ideas of how to solve this particular alignment issue, dynamically or not.


text centered, but since it didn't work, I gave up on trying anything else through TikZ. It's also a great solution, Gonzalo! Thanks! – Paulo Cereda Jun 26 '11 at 10:36