6

I've been using zref to handle my references. It has allowed me to customize a reference the way I want through its property lists. In particular, I reference a variety of structures in my document that are created in an automated way. Here's a MWE that provides the setup and illustrates my current problem:

\documentclass{article}
\usepackage{zref}% http://ctan.org/pkg/zref
\makeatletter
\newcounter{thegraph}% Graph counter
\zref@newlist{graph}% Graph reference stored in list 'graph'
\zref@newprop*{type}[??]{??}% Create new 'graph type' property
\zref@newprop*{number}[??]{\thethegraph}% Create new 'graph number' property
\zref@addprops{graph}{type, number}% Graph reference (or property list) has type & number
\newcommand{\graph}[1]{\zref@setcurrent{type}{G}\newgraph{1}{#1}}%
\newcommand{\digraph}[1]{\zref@setcurrent{type}{D}\newgraph{2}{#1}}%
\newcommand{\newgraph}[2]{% New graph #1=type; #2=ref
  \refstepcounter{thegraph}% Increase counter
  % zref label has to written immediately (not at page shipout), since more than one new graph may appear on page
  \zref@wrapper@immediate{\zref@labelbylist{#2}{graph}}% Write graph list to .aux (using zref)
  \label{#2}% Label newgraph for reference
  \ifnum#1>0\ensuremath{\zref@getcurrent{type}_{\thethegraph}}\fi% Print graph symbol (if requested)
}
\newcommand{\graphref}[1]{\ensuremath{\zref@extract{#1}{type}_{\zref@extract{#1}{number}}}}% Reference to a graph
\newcommand{\complementg@aux}[2][3]{{}\mkern#1mu\overline{\mkern-#1mu#2}}
\newcommand{\complementg}[2][3]{\edef\@tempa{#2}\expandafter\complementg@aux\expandafter[\expandafter#1\expandafter]\@tempa}
\makeatother

\begin{document}

This is~\graph{first} and~\digraph{second}. Now see~\graphref{first} and~\graphref{second}.
What about~$\complementg{\graphref{first}}$ and~$\complementg{\graphref{second}}$?

\end{document}

The above MWE gives the following output

enter image description here

while I'd like it to look like this:

enter image description here

This is~$G_1$ and~$D_2$. Now see~$G_1$ and~$D_2$.
What about~$\complementg{G_1}$ and~$\complementg{D_2}$?

The choice of \complementg stems from a discussion in chat and because it worked in a non-zref implementation.

My question is: How can I define \complementg such that allows for being used in all of the following formats while only providing a "modified \overline" for the first character:

$\complementg{G_1}$% Basic math content with subscript manual subscript
$\complementg{\graphref{lab}}$% Referenced zref label (like above)
$\complementg{\mathcal{G}}$% "Complex" formatting of content

In a typographic sense, this question is somewhat related to What's the proper way to write a vector with a single character subscript?

Werner
  • 603,163

1 Answers1

5

Another pearl for my don't-use-\ensuremath-unless-really-necessary campaign.

\documentclass{article}
\usepackage{zref}

\makeatletter
\newcounter{thegraph}% Graph counter
\zref@newlist{graph}% Graph reference stored in list 'graph'
\zref@newprop*{type}[??]{??}% Create new 'graph type' property
\zref@newprop*{number}[??]{\thethegraph}% Create new 'graph number' property
\zref@addprops{graph}{type, number}% Graph reference (or property list) has type & number

\newcommand{\graph}[1]{\zref@setcurrent{type}{G}\newgraph{1}{#1}}%

\newcommand{\digraph}[1]{\zref@setcurrent{type}{D}\newgraph{2}{#1}}%

\newcommand{\newgraph}[2]{% New graph #1=type; #2=ref
  \refstepcounter{thegraph}% Increase counter
  % zref label has to written immediately (not at page shipout),
  % since more than one new graph may appear on page
  \zref@wrapper@immediate{\zref@labelbylist{#2}{graph}}% Write graph list to .aux (using zref)
  \label{#2}% Label newgraph for reference
  % Print graph symbol (if requested)
  \ifnum#1>\z@\ensuremath{\zref@getcurrent{type}_{\thethegraph}}\fi
}
%%% DON'T use \ensuremath here
\newcommand{\graphref}[1]{\zref@extract{#1}{type}_{\zref@extract{#1}{number}}}% Reference to a graph
\newcommand{\complementg@aux}[2][3]{{}\mkern#1mu\overline{\mkern-#1mu#2}}

%%% Less clumsy definition of \complementg    
\newcommand{\complementg}[2][3]{%
  \begingroup\edef\x{\endgroup
  \unexpanded{\complementg@aux[#1]}#2}\x}
\makeatother

\begin{document}

This is~\graph{first} and~\digraph{second}. 
Now see~$\graphref{first}$ and~$\graphref{second}$.
What about~$\complementg{\graphref{first}}$ 
and~$\complementg{\graphref{second}}$?

\end{document}

enter image description here

egreg
  • 1,121,712
  • Perhaps this is the pearl for me, since I've never seen it to be the problem you perpetually purport. :) The reason for using it stems from a document I've been working on for years... as such, since the problem didn't creep up until recently, I didn't give it much attention. Thanks... – Werner Apr 20 '13 at 14:56