8

using the following code, when I click on references (having red boxes), they do not refer to proper places.

\documentclass{article}
\usepackage{array,longtable}
\usepackage{hyperref}

\begin{document}

\renewcommand\theenumii{\alph{enumii}}
\makeatletter
\def\subitem{\omit\@arstrut 
\ifnum\value{enumii}=0 \stepcounter{enumi}\fi
\refstepcounter{enumii}\theenumi\theenumii\hfill}
\def\item{\omit\setcounter{enumii}{0}\let\omit\relax}
\makeatother

\begin{longtable}{>{\refstepcounter{enumi}\setcounter{enumii}{0}\theenumi}lr}
\item
\subitem \label{1a}& fooo \\
\subitem \label{1b}& foo \\
\item
\subitem \label{2a}& foo \\
\subitem \label{2b}& foo \\
\label{3}& foo  \\
\label{4}& foooo \\
\end{longtable}

we have \ref{1a}, \ref{2b}, \ref{4}

\end{document}
2
  • Be careful in using \subitem as a command name, since it's used in the theindex environment. All's well until you need to make an index, when disaster might be approaching.
    – egreg
    Commented Nov 30, 2012 at 7:57
  • 1
    @egreg yes well he got that code from me in the previous question.:-) Once it's working the thing to do really is put the \item and \subitem definitions into the start code of a custom environment just before starting longtable, to keep the scope of the redefinition local. Commented Nov 30, 2012 at 9:23

1 Answer 1

12

Two problems:

  • The anchor names are not unique. This can be fixed by redefining \theHenumi and \theHenumii.
  • The anchor position is the baseline. The anchor is set by \refstepcounter. This can be cured by \raisebox{\ht\@arstrutbox}[0pt]{...}. But then \refstepcounter is called inside a group and the meaning of \@currentlabel (what \ref sees) and \@currentHref (anchor name, if option localanchorname is used). Therefore these values are put into global scratch variables to restore them after \raisebox.

The example file with update for adding parentheses and the dot. And another update for bold numbers in the table.

\documentclass{article}
\usepackage{array,longtable}
\usepackage[verbose]{hyperref}

\begin{document}

\renewcommand\theenumii{(\alph{enumii})}
\renewcommand*{\theHenumi}{\theenumi}
\renewcommand*{\theHenumii}{\theenumi\alph{enumii}}
\makeatletter
\newcommand*{\raiserefstepcounter}[1]{%
  \raisebox{\ht\@arstrutbox}[0pt]{%
    \hypersetup{localanchorname}%
    \refstepcounter{#1}%
    \global\let\g@currentlabel\@currentlabel
    \global\let\g@currentHref\@currentHref
  }%
  \let\@currentlabel\g@currentlabel
  \let\@currentHref\g@currentHref
}
\def\subitem{%
  \omit\@arstrut
  \ifnum\value{enumii}=0 %
    \stepcounter{enumi}%
  \fi
  \raiserefstepcounter{enumii}%
  \bfseries
  \theenumi\theenumii.\hfill
}
\def\item{%
  \omit
  \setcounter{enumii}{0}%
  \let\omit\relax
}
\makeatother

\makeatletter
\begin{longtable}{%
  >{%
    \raiserefstepcounter{enumi}%
    \setcounter{enumii}{0}%
    \bfseries\theenumi
  }l<{\unskip.}r%
}
\item
\subitem \label{1a}& fooo \\
\subitem \label{1b}& foo \\
\item
\subitem \label{2a}& foo \\
\subitem \label{2b}& foo \\
\label{3}& foo  \\
\label{4}& foooo \\
\end{longtable}

we have \ref{1a}, \ref{2b}, \ref{4}

\end{document}

Result

6
  • thanks for sorting that out, I asked the OP to ask another question rather than adding a hyperref constraint on the previous one. Commented Nov 30, 2012 at 9:26
  • thanks. in the table, we have 1a, 1b, for example. how can i set them to 1(a)., 1(b).. also, in the body of text, when we want to refer to these items, we would have we have 1(a) and 1(b). it would be preferable not to use \renewcommand{\theenumii}{...}
    – smh
    Commented Nov 30, 2012 at 11:01
  • @shm It is unclear to me, what you want to have in the table, with/without parentheses, with/without dot? Commented Nov 30, 2012 at 11:17
  • @Oberdiek i want 1(a)., 1(b)., 2., ... in the table (list items). and when referring to these items out in the body of text, i want we have 1(a) or 1(b) or 2, for example.
    – smh
    Commented Nov 30, 2012 at 12:15
  • @Oberdiek thanks a lot. as an extension, could we have numbers in 1'st column of table in bold, and blue colored form?
    – smh
    Commented Nov 30, 2012 at 22:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .