For Programmers: Free Programming Magazines  


Home > Archive > Tex > July 2006 > Cross-referencing between chapters









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Cross-referencing between chapters
seldnplan@gmail.com

2006-07-25, 7:04 pm

Hi there,

I'm making some notes now that I'm spreading over a few chapters, each
contained in a distinct tex file and then included in the master file
using the input command. I'm numbering my Theorems, etc. by section
number, but when I reference them (I'm using pdfTex with hyperref), I
only get the section number and not the chapter number. To be more
precise, if I have the following code:
-----------------
\documentclass{amsbook}
\usepackage{xr-hyper}
\usepackage[plainpages=false,pdfpagelabe
ls]{hyperref}
\newtheorem{thm}{Theorem}[section]

\begin{document}

\chapter{Chapter One}
\input{a1.tex}

\chapter{Chapter Two}
\input{a2.tex}

\end{document}
--------
%a1.tex

\section{bla}

\section{blah}\labe{a1:secn:blah}

\begin{thm}\label{a1:thm:blah}
\end{thm}
------
%a2.tex:

\ref{a1:thm:blah}

\ref{a1:secn:blah}
-------------

The output I get in the second chapter is
2.1
2,
instead of the less confusing
1.2.1
1.2

So how can I get this two-level cross referencing? For intra-chapter
referencing, I'm happy with what I'm getting now; it's the
inter-chapter referencing that's a bit of a mess.

Thanks,
Keerthi

seldnplan@gmail.com

2006-07-25, 7:04 pm


Enrico Gregorio wrote:
> <seldnplan@gmail.com> wrote:
>
>
> It is less confusing to you who are writing the book. There will be no
> Theorem 1.2.1 in the book, after all. Usually I prefer something like
> "as we saw in Theorem~\ref{a1:thm:blah} in chapter~\ref{a1}".


Well, it's probably less confusing for me than for the reader.
Especially when there are three different Theorems 2.1. That's mainly
the reason I'd like a two level referencing system.

> If you want to do that, you have some choices. If the book is not so
> huge that you cannot remember if the referenced theorem is in this
> chapter or not, "\ref{a1}.\ref{a1:thm:blah}" will suffice. Of course
> after having assigned \label{a1} to the first chapter.


This makes sense, but I was wondering if there was a way of defining a
reference command so that it would work like I wanted. The only thing I
don't like about your solution is that the references in the pdf file
will have two red boxes each: one for the chapter and the other for the
actual reference. But I guess that won't be a problem in the hard copy!

Thanks,
Keerthi

Enrico Gregorio

2006-07-25, 7:04 pm

<seldnplan@gmail.com> wrote:

> This makes sense, but I was wondering if there was a way of defining a
> reference command so that it would work like I wanted. The only thing I
> don't like about your solution is that the references in the pdf file
> will have two red boxes each: one for the chapter and the other for the
> actual reference. But I guess that won't be a problem in the hard copy!


I looked in the TeX catalog at <http://texcatalogue.sarovar.org>, but
unfortunately the problem has not been addressed.

I tried this patch:

======
\newcount\phchap
\def\eghide#1.{\ifnum#1=\phchap\else#1.\fi}

\makeatletter
\let\old@chapter\@chapter
\def\@chapter{\global\advance\phchap 1\relax\old@chapter}
\makeatother

% uncomment the following if you don't want that sections
% get the chapter number in front of them
%\renewcommand{\thesection}{\protect\egh
ide\thechapter.\arabic{section}}

\newtheorem{thm}{Theorem}[section]
\renewcommand{\thethm}{\protect\eghide\t
hechapter.%
\arabic{section}.\arabic{thm}}
======

Now, with little testing, something like the following is working.

======
\chapter{First}

\section{One}

\begin{thm}\label{a+b}
$a+b=b+a$.
\end{thm}

Theorem~\ref{a+b} is important

\chapter{Second}

\section{One}
By Theorem~\ref{a+b}, we get that something happens.
======

Try it. At least you don't need to change any command. And let me
know if it works.

Ciao
Enrico
J.S.Milne

2006-07-25, 7:04 pm

> This makes sense, but I was wondering if there was a way of defining a
> reference command so that it would work like I wanted. The only thing I
> don't like about your solution is that the references in the pdf file
> will have two red boxes each: one for the chapter and the other for the
> actual reference. But I guess that won't be a problem in the hard copy!
> Keerthi


You may find the package smartref useful.

Enrico Gregorio

2006-07-25, 7:04 pm

J.S.Milne <ggl@jmilne.org> wrote:

>
> You may find the package smartref useful.


It doesn't do what the OP is trying to get. At least not quite easily
and surely with the double reference. I'm sure that my solution is far
from general, but it should work (not sure about equations in amsmath,
though).

Ciao
Enrico
Mariano Suárez-Alvarez

2006-07-25, 7:04 pm


seldnplan@gmail.com wrote:
> Enrico Gregorio wrote:
>
> Well, it's probably less confusing for me than for the reader.
> Especially when there are three different Theorems 2.1. That's mainly
> the reason I'd like a two level referencing system.


As a reader who has spent way too much time browsing poorly
cross referenced books, I think using roman numbers for the
chapters, so as to make very obvious when you put the
chapter number in a cross reference, would be very helpful
if you do this.

Regards,

-- m

seldnplan@gmail.com

2006-07-25, 10:03 pm


Enrico Gregorio wrote:
> <seldnplan@gmail.com> wrote:
>
>
> I looked in the TeX catalog at <http://texcatalogue.sarovar.org>, but
> unfortunately the problem has not been addressed.
>
> I tried this patch:
>
> ======
> \newcount\phchap
> \def\eghide#1.{\ifnum#1=\phchap\else#1.\fi}
>
> \makeatletter
> \let\old@chapter\@chapter
> \def\@chapter{\global\advance\phchap 1\relax\old@chapter}
> \makeatother
>
> % uncomment the following if you don't want that sections
> % get the chapter number in front of them
> %\renewcommand{\thesection}{\protect\egh
ide\thechapter.\arabic{section}}
>
> \newtheorem{thm}{Theorem}[section]
> \renewcommand{\thethm}{\protect\eghide\t
hechapter.%
> \arabic{section}.\arabic{thm}}
> ======
>
> Now, with little testing, something like the following is working.
>
> ======
> \chapter{First}
>
> \section{One}
>
> \begin{thm}\label{a+b}
> $a+b=b+a$.
> \end{thm}
>
> Theorem~\ref{a+b} is important
>
> \chapter{Second}
>
> \section{One}
> By Theorem~\ref{a+b}, we get that something happens.
> ======
>
> Try it. At least you don't need to change any command. And let me
> know if it works.


It works perfectly. Thanks! BTW, what would be a good reference for
this kind of thing if I had to do it by myself? I assume it would be
some TeX programming guide? The syntax is just a tad bit alien, but
it's mostly all the escapes that throw me off.

Thanks,
Keerthi

seldnplan@gmail.com

2006-07-25, 10:03 pm


seldnplan@gmail.com wrote:

> It works perfectly. Thanks! BTW, what would be a good reference for
> this kind of thing if I had to do it by myself? I assume it would be
> some TeX programming guide? The syntax is just a tad bit alien, but
> it's mostly all the escapes that throw me off.


Is this a good reference?

http://www.eijkhout.net/tbt/

It's Eijkhout's Tex by Topic.

Enrico Gregorio

2006-07-26, 4:08 am

<seldnplan@gmail.com> wrote:

> seldnplan@gmail.com wrote:
>
>
> Is this a good reference?
>
> http://www.eijkhout.net/tbt/
>
> It's Eijkhout's Tex by Topic.


That's for some of the things, mostly for TeX programming. LaTeX is
based on TeX, as you know. The LaTeX Companion is a good friend for
a start in LaTeX programming.

I used in the hack for you some Plain TeX construction just for the
sake of speed. For example the counter is defined as a TeX counter
since I had to be sure that it is stepped globally; some authors
use horrors like \begin{chapter}...\end{chapter}, you see... :-)

Ciao
Enrico
Ulrich Diez

2006-07-26, 8:03 am

Keerthi wrote:

[...]
> I'm numbering my Theorems, etc. by section number, but when I
> reference them

[...]
> I only get the section number and not the chapter number

[...]
> -----------------
> \documentclass{amsbook}
> \usepackage{xr-hyper}
> \usepackage[plainpages=false,pdfpagelabe
ls]{hyperref}
> \newtheorem{thm}{Theorem}[section]
>
> \begin{document}
>
> \chapter{Chapter One}
> \input{a1.tex}
>
> \chapter{Chapter Two}
> \input{a2.tex}
>
> \end{document}
> --------
> %a1.tex
>
> \section{bla}
>
> \section{blah}\labe{a1:secn:blah}
>
> \begin{thm}\label{a1:thm:blah}
> \end{thm}
> ------
> %a2.tex:
>
> \ref{a1:thm:blah}
>
> \ref{a1:secn:blah}
> -------------
>
> The output I get in the second chapter is
> 2.1
> 2,
> instead of the less confusing
> 1.2.1
> 1.2
>
> So how can I get this two-level cross referencing? For intra-chapter
> referencing, I'm happy with what I'm getting now; it's the
> inter-chapter referencing that's a bit of a mess.



For exhibiting the result of referencing, you don't need to
split your "minimal-example" into multiple \input-files. That's
unneeded complexity.


Keerthi also wrote:
[...]
> The only thing I don't like

[...]
> is that the references in the pdf file will have two red boxes each

[...]


I can see two solutions:

Solution 1:
===========

As Enrico Gregorio already pointed out, you can use two references
instead of one:

"\ref{a1}.\ref{a1:thm:blah}"

If you wish to have only one red box, you can use hyperref's
"\hyperref"-macro. The first [optional] argument takes the
label, the second {mandatory} argument takes the text for the
link.
Within the mandatory link-text-argument you can use hyperref's
"starred" versions of \ref and \pageref which like \ref and
\pageref deliver the referenced numbers but unlike \ref/pageref
do not create hyperlinks to the associated sectional units
(which would be a bad idea within the text-argument of a
hyperlink-creating macro...):

"\hyperref[a1:thm:blah]{\ref*{a1}.\ref*{a1:thm:blah}}"

You may wish to generate a link- and colorless version of your
elaborate for printing hard-copies. For achieving this, some
people simply "comment out" the line
"\usepackage[...]{hyperref}".

Don't do that. Better just supply the "draft"-option to hyperref
as this way you keep all hyperref-macros, e.g., \phantomsection,
\ref*, \pageref*... This way also no hyperlinks are created, thus
this way also the text does not get "colorized".


Solution 2:
===========

If you wish to automatize referencing, you can do so by changing
"\p@thm". "\p@thm" by default is empty and is to hold the prefix/
leading stuff which gets added when the value of the counter
"thm" is written into an aux-file-\newlabel-entry due to a call
to "\label" within your TeX-file.
Every counter defined in terms of \newcounter has such a
\p@<counter>-macro.

You can redefine "\p@thm" and "\p@section" to print the
chapter-number also:

\makeatletter
\renewcommand*\p@thm{\thechapter.}
\let\p@section=\p@thm
\makeatother

If you want to have added both leading and trailing stuff to
your reference, you can achieve this as follows:

\makeatletter
\renewcommand*\p@thm{\expandafter\innerp
@thm}%
\newcommand\innerp@thm[1]{%
<leading stuff>
#1%
<trailing stuff>
}%
\makeatother

#1 will be the referenced number.
Both within <leading stuff> and within <trailing stuff> you can
have code for forking whether referencing takes place in the
chapter where the label was placed.

All you have to obey is that the content of \innerp@thm (except
\thepage) gets evaluated fully for writing to the aux-file at the
time when the label-command occurs within the text. Thus you have
to use a "\protect" here and there to make sure that the command
is not evaluated at writing-time but written to the aux-file
verbatim so that it can be evaluated at referencing-time.

If you simply compare the value of the chapter-counter at
"label"-time to the value of the chapter-counter at
"reference-time" for forking, this may be wrong as soon as you
use macros like \appendix or \backmatter which reset the chapter-
counter. Then you also need to check if still in front- or main-
or backmatter...


\documentclass{amsbook}
\usepackage{xr-hyper}
\usepackage[plainpages=false,pdfpagelabe
ls]{hyperref}

\newtheorem{thm}{Theorem}[section]

\makeatletter

% mechanism for detecting if in front- or main- or backmatter:

\newcommand\@currentmatter{}%
\newcommand\setfrontmatter{\gdef\@curren
tmatter{f}}%
\newcommand\setmainmatter{\gdef\@current
matter{m}}%
\newcommand\setbackmatter{\gdef\@current
matter{b}}%

% some argument-grabbers:

\@ifundefined{@lastthreeoffour}%
{\long\gdef\@lastthreeoffour#1#2#3#4{#2#
3#4}}%
\relax
\@ifundefined{@secondofthree}%
{\newcommand\@secondofthree[3]{#2}}%
\relax
\@ifundefined{@threeofthree}%
{\newcommand\@threeofthree[3]{#1#2#3}}%
\relax

% let's redefine \p@thm:

\renewcommand\p@thm{%
\expandafter\innerp@thm
}%
\newcommand\innerp@thm[1]{%
\protect\ifnum
\protect\value{chapter}=\number\c@chapte
r\space
\protect\expandafter\protect\@firstofone

\protect\else
\protect\expandafter\protect\@lastthreeo
ffour
\protect\fi
{%
\protect\if\@currentmatter\protect\@curr
entmatter\space
\protect\expandafter\protect\@secondofth
ree
\protect\else
\protect\expandafter\protect\@threeofthr
ee
\protect\fi
}%
{[leading stuff: chapter \thechapter] }%<-space at end!
{#1}%
{ [trailing stuff: chapter \thechapter]}%<-space in front!
}%
%
\let\p@section=\p@thm
%
\makeatother

\begin{document}

\mainmatter\setmainmatter

\chapter{Chapter One}

\section{bla}
\section{blah}\label{a1:secn:blah}

\begin{thm}\label{a1:thm:blah}
This is my theorem.
\end{thm}

theorem-reference in same chapter: \ref{a1:thm:blah}


section-reference in same chapter: \ref{a1:secn:blah}

\chapter{Chapter Two}

theorem-reference in other chapter: \ref{a1:thm:blah}

section-reference in other chapter: \ref{a1:secn:blah}


\appendix\setbackmatter

\chapter{Some appendix}

theorem-reference in main-matter: \ref{a1:thm:blah}

section-reference in main-matter: \ref{a1:secn:blah}

\end{document}

If you are interested in customizing your references, you might
also be interested in my last release of custref.sty which can
be found by searching the google-groups-archives.
I actually take custref.sty for obsolete and am not inclined to
put it on CTAN as you can always go the
\hyperref[<label>]{text}-approach of solution 1.


Ulrich


Mycroft

2006-07-26, 7:05 pm


seldnplan@gmail.com wrote:
> Hi there,
>
> I'm making some notes now that I'm spreading over a few chapters, each
> contained in a distinct tex file and then included in the master file
> using the input command. I'm numbering my Theorems, etc. by section
> number, but when I reference them (I'm using pdfTex with hyperref), I
> only get the section number and not the chapter number. To be more
> precise, if I have the following code:
> -----------------
> \documentclass{amsbook}
> \usepackage{xr-hyper}
> \usepackage[plainpages=false,pdfpagelabe
ls]{hyperref}
> \newtheorem{thm}{Theorem}[section]
>
> \begin{document}
>
> \chapter{Chapter One}
> \input{a1.tex}
>
> \chapter{Chapter Two}
> \input{a2.tex}
>
> \end{document}
> --------
> %a1.tex
>
> \section{bla}
>
> \section{blah}\labe{a1:secn:blah}
>
> \begin{thm}\label{a1:thm:blah}
> \end{thm}
> ------
> %a2.tex:
>
> \ref{a1:thm:blah}
>
> \ref{a1:secn:blah}
> -------------
>
> The output I get in the second chapter is
> 2.1
> 2,
> instead of the less confusing
> 1.2.1
> 1.2
>
> So how can I get this two-level cross referencing? For intra-chapter
> referencing, I'm happy with what I'm getting now; it's the
> inter-chapter referencing that's a bit of a mess.
>
> Thanks,
> Keerthi



The theorem environment defines a reference to it as the first number
of the level of sectioning you specify to reset the counter, followed
by the number of the theorem. In this case "section"."thm". This
doesn't make sense to me. A simple fix is to put chapter instead of
section in your "\newtheorem" command. This will reset the counter at
the beginning of each chapter, hence give you 1.1 for the theorem.

If you really want to reset on section, then doesn't just simply
renewing \thethm work? As in:

\renewcommand{\thethm}{\arabic{chapter}.\arabic{section}.\arabic{thm}}

My two cents... :)

Mycroft

J.S.Milne

2006-07-26, 7:05 pm

Mariano Su=E1rez-Alvarez wrote:

> As a reader who has spent way too much time browsing poorly
> cross referenced books, I think using roman numbers for the
> chapters, so as to make very obvious when you put the
> chapter number in a cross reference, would be very helpful
> if you do this.
>


Yes, the correct way to do these things is:
(a) Use Cap roman numerals for the Chapter numbers
(b) Don't use the Chapter number to number things within a chapter or
for intra-chapter references.
(c) Do use the Chapter number for inter-chapter references.
The package smartref makes it easy to do this.

Ulrich Diez

2006-07-27, 7:03 pm

My previous suggestions implied writing the whole forking-
mechanism to aux-file each time when \label was applied to
the thm-counter.
That is unneeded complexity also. Sorry.

Ulrich

\documentclass{amsbook}
\usepackage{xr-hyper}
\usepackage[plainpages=false,pdfpagelabe
ls]{hyperref}

\newtheorem{thm}{Theorem}[section]

\makeatletter

% mechanism for announcing a reset of the chapter-counter:

\newcommand*\@currentmatter{0}%
\DeclareRobustCommand\ChapterWasReset{%
{%
\@tempcnta=\@currentmatter
\advance\@tempcnta1%
\xdef\@currentmatter{\number\@tempcnta}%

}%
}%

% some argument-grabbers and forking:

\providecommand\@lastthreeoffour[4]{#2#3
#4}%
\providecommand\@thirdoffour[4]{#3}%
%
\@ifdefinable\@checkforsamechapter{%
\DeclareRobustCommand*\@checkforsamechap
ter[2]{%
\ifnum\value{chapter}=#1 %
\expandafter\@firstofone
\else
\expandafter\@lastthreeoffour
\fi
{%
\ifnum\@currentmatter=#2 %
\expandafter\@thirdoffour
\else
\expandafter\@lastthreeoffour
\fi
\relax
}%
}%
}%

% let's redefine \p@thm:

\renewcommand\p@thm{%
\expandafter\innerp@thm
}%
\newcommand\innerp@thm[1]{%
\@checkforsamechapter{\number\c@chapter}
{\@currentmatter}%
{[leading stuff: chapter \thechapter] }%<-space at end!
{#1}%
{ [trailing stuff: chapter \thechapter]}%<-space in front!
}%
%
\let\p@section=\p@thm
%
\makeatother

\begin{document}

\chapter{Chapter One}

\section{bla}
\section{blah}\label{a1:secn:blah}

\begin{thm}\label{a1:thm:blah}
This is my theorem.
\end{thm}

theorem-reference in same chapter: \ref{a1:thm:blah}

section-reference in same chapter: \ref{a1:secn:blah}

\chapter{Chapter Two}

theorem-reference in other chapter: \ref{a1:thm:blah}

section-reference in other chapter: \ref{a1:secn:blah}

\appendix\ChapterWasReset

\chapter{Some appendix}

theorem-reference in main-matter: \ref{a1:thm:blah}

section-reference in main-matter: \ref{a1:secn:blah}

\end{document}


J.S. Milne

2006-07-27, 7:03 pm

Your code is very useful --- thanks!

Ulrich Diez

2006-07-28, 8:04 am

J.S. Milne wrrote:
> Your code is very useful --- thanks!


<OT>
I feel a bit abashed for the need of posting three times/several
times until getting my approach to the problem into a (more or
less) useful fashion. With other discussions at c.t.t. I happened
to do the same. So maybe my "irony-detector" is a bit hypersensitive
these days. One reason for me to post repeatedly is that some day
somebody might browse some usenet-archive in order to find a
solution to the same problem. That person shall not only find my
"earlier" approaches but also what I take for more useful.

Be that as it may.
</OT>

Be aware that my code does break functionality of Heiko Oberdiek's
refcount-package: That package relies on getting plain numbers
from a \newlabel-entry. (But if you use that package, you can use its
macros for creating your own macro which -for forking - extracts
the reference-number and afterwards the chapter-number from
the reference-number and compares that to the current
chapter-value...)

Also be aware that when using my code together with hyperref, you
cannot use <leading stuff> or <trailing stuff> for the automatic
generation of footnotes or the like: <leading stuff> and <trailing stuff>
will be part of the hyperlink created by the reference. Footnotes also
are hyperlinked. So putting a footnote into <leading stuff> or
<trailing stuff> will mean creating a hyperlink within a hyperlink
while "nested links" currently are not supported by any hyperref-driver.

Ulrich

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com