Generalsections inside longtable: proper referencing

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
akopts
Posts: 3
Joined: Sat Dec 13, 2008 4:35 am

sections inside longtable: proper referencing

Post by akopts »

My document consists of a longtable,
which contains sections and subsesctions (shown in multicolumns).
They are defined implicitly, e.g., not via \section, but via
\refstepcounter{section}\addcontentsline{toc}{section}{\protect\numberline{\thesection}#1}

Each row in the table is a subsubsection. When I attempt to refer
to a row, I always get \thetable as a reference,
whereas I want \thesection\thesubsection.\thesubsubsection.

How do I redefine \label so that it would put the above sequence to the .aux file?
Spent lots of time searching, cannot find any clue.
Any other approaches?

In relation to that, is it possible to print definition of arbitrary LaTeX
command which is defined as a macro? It would be very convenient
for the purpose of redefining things...

Thanks,
Alex

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Re: sections inside longtable: proper referencing

Post by phi »

Please post a minimal working example. \label normally always refers to the last occurrence of \refstepcounter, so this should work.
Regarding your second question: \meaning\foo expands to the definition of \foo, whereas \show\foo prints the definition of \foo on the terminal.
akopts
Posts: 3
Joined: Sat Dec 13, 2008 4:35 am

sections inside longtable: proper referencing

Post by akopts »

Here is a working example. Both labels refer to the table number,
whereas I want \ref to print 1.1.1 and 2.2.1

Thanks,
Alex

Code: Select all

\documentclass{report}
\usepackage{longtable}
\usepackage{hyperref}
\usepackage{ifthen}
\setcounter{tocdepth}{3}
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}}
\newcommand{\padding}{7pt}
\newcommand{\entry}[4]{
  \refstepcounter{subsubsection}
  \parbox{13mm}{\vspace*{\padding}\centering \ifthenelse{\equal{\thesection}{0}}{}{\thesubsubsection} \vspace*{\padding}} &
  \parbox{25mm}{\vspace*{\padding}\raggedright #1 \vspace*{\padding}} &
  \parbox{10mm}{\vspace*{\padding}\centering #2 \vspace*{\padding}} &
  \parbox{30mm}{\vspace*{\padding}\raggedright #3 \vspace*{\padding}} &
  \parbox{40mm}{\vspace*{\padding}\raggedright #4 \vspace*{\padding}} \\ \hline
}
\newcommand{\sect}[1]{
  \hline\multicolumn{5}{|c|}{
    \refstepcounter{section}
    \addcontentsline{toc}{section}{\protect\numberline{\thesection}#1}
    \parbox{128mm}{
      \vspace*{\padding}\centering\large\bf \thesection. #1 \vspace*{\padding}
    }
  }\\ \hline
}
\newcommand{\subsect}[1]{
  \hline\multicolumn{5}{|c|}{
    \refstepcounter{subsection}
    \addcontentsline{toc}{subsection}{\protect\numberline{\thesubsection}#1}
    \parbox{128mm}{
      \vspace*{\padding}\centering \thesubsection. #1 \vspace*{\padding}
    }
  }\\ \hline
}
\begin{document}
\tableofcontents
\begin{longtable}{|l|l|l|l|l|}
\hline\hline
\entry{\bf c1}{\bf c2}{\bf c3}{\bf c4}
\hline\hline
\endhead
\sect{Section 1}
\subsect{Subsection 1}
\entry{a}{b}{\label{l1} c}{d}
\entry{e}{f}{\ref{l1} g}{h}
\sect{Section 2}
\subsect{Subsection 1}
\subsect{Subsection 2}
\entry{m}{n}{\label{l2} o}{p}
\entry{i}{j}{\ref{l2} k}{l}
\end{longtable}
\end{document}
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

sections inside longtable: proper referencing

Post by phi »

OK, this is a tough one. Seems as if longtable puts each cell in a separate box or group, so that assignments are local to the cell. Here is a hack to define and restore the current label text globally:

Code: Select all

\makeatletter
\newcommand{\entry}[4]{
  \stepcounter{subsubsection}\global\let\old@currentlabel\@currentlabel\xdef\@currentlabel{\thesubsubsection}%
  \parbox{13mm}{\vspace*{\padding}\centering \ifthenelse{\equal{\thesection}{0}}{}{\thesubsubsection} \vspace*{\padding}} &
  \parbox{25mm}{\vspace*{\padding}\raggedright #1 \vspace*{\padding}} &
  \parbox{10mm}{\vspace*{\padding}\centering #2 \vspace*{\padding}} &
  \parbox{30mm}{\vspace*{\padding}\raggedright #3 \vspace*{\padding}} &
  \parbox{40mm}{\vspace*{\padding}\raggedright #4 \vspace*{\padding}} \global\let\@currentlabel\old@currentlabel \\ \hline
}
\makeatother
akopts
Posts: 3
Joined: Sat Dec 13, 2008 4:35 am

Re: sections inside longtable: proper referencing

Post by akopts »

Wow! Thanks!
Post Reply