Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am involved in a writing a large document with a co-author who set up the LaTex files that we're using (i.e., I don't have complete autonomy in what I change).

We have a root file which does two things:

1) loads a header file that contains the \usepackage{} commands and then lists the user-defined commands.

2) Then it loads in each chapter file using a user-defined command \Chapter{#}{Title}

Which is coded as:

\newcommand{\Chapter}[2]{
\setcounter{chapter}{\numexpr#1-1}%
\refstepcounter{chapter}%
\setcounter{eqnhelper}{0}
\setcounter{section}{0}
\setcounter{figure}{0}
\setcounter{equation}{0}
\include{#2}
}

As you can see, this command mostly just resets various counters to 0, it also has the command \setcounter{chapter}{#1}. That way, when we compile chapters 3 and 8, they are both numbered correctly. Last, it calls the chapter file with the \include command.

Each chapter file starts off with

% !TEX root = 00_makefile.tex 
\chapter*{Chapter \thechapter\\ Chapter Title}
\label{chapter: apps of integration}

Here's the problem. Let's say we compile two chapters (say #3 and 8), and we want to reference Chapter 3 up in Chapter 8. Using the Typing Chapter \ref{chapter_label} command produces: Chapter <-(see the blank space?). Why is that? and how do we fix it?

I guess I should also mention that this is using \documentclass{report} and we are NOT using the hyperref package.

UPDATE: I just now realized I can't reference the chapter within itself either. Inside chapter 3 I added the line This is chapter \ref{chapter3} and I still got a blank space after the word 'chapter.'

share|improve this question
    
It would help to have access to the definition of \Chapter. –  Werner Dec 29 '11 at 20:26
    
@Werner: Sorry about that. I was trying to keep the question short, but still complete. I thought by saying all it did was reset the counters and call the chapter file would be enough. Even though you already solved my problem, I'll edit the question so it is more complete in case other newbies need the help. –  HTG Dec 29 '11 at 20:42

2 Answers 2

up vote 4 down vote accepted

Correct chapter references require something like \refstepcounter{chapter} to both step the chapter counter (increment it), and add an appropriate anchor for referencing. You're lacking the latter (since setting it is similar to stepping, taking care of the former).

For a quick fix, you should try to change the command \setcounter{chapter}{#1} to

\setcounter{chapter}{\numexpr#1-1}%
\refstepcounter{chapter}%

This would set the counter chapter to one less than it is needed, and then perform the necessary "step-and-anchor" required for correct referencing. If you don't have access to these files, they may be patched using etoolbox if the appropriate command containing the problem is known.

On a side note: Try to avoid spaces within labels (or control sequences in general). For example, rather use \label{chapter:apps-of-integration}.

share|improve this answer
2  
Thanks! That worked. I find it (somewhat) annoying that all the other online 'tutorials' and references I checked neglected to mention the need for \refstepcounter. –  HTG Dec 29 '11 at 20:30

The mechanism for including the chapters may not be the best way to do this. Have you thought about letting LaTeX do the work?

For example, if you write each chapter in its own file, you can include them with the \include command. The \includeonly command can be used to determine which chapter files should be really included and which ones should not.

The advantage of this mechanism is that cross-referencing will still work, provided you've included the cross-referenced file at least once.

\includeonly{Introduction,FirstChapter}

...

\begin{document}

\include{Introduction}  % included.
\include{FirstChapter}  % included.
\include{SecondChapter} % not included.

\end{document}

Here Introduction.tex, FirstChapter.tex, and SecondChapter.tex are LaTeX files that may be included in the body of the document. The \includeonly command tells LaTeX that only Introduction.tex and FirstChapter.tex should be included. In FirstChapter.tex you may reference labels that are defined and SecondChapter.tex and you get the proper numbers/letters for the labels if put SecondChapter in the argument list of \includeonly in some previous LaTeX run.

For more information, see UK TUG FAQ.

share|improve this answer
    
Does this mean that when I compile with \includeonly{SecondChapter} it still sets the chapter counter to 2? –  HTG Dec 29 '11 at 20:39
    
I tried this, but each chapter is numbered 0. Do I need to include the \refstepcounter at the beginning of each chapter document? or something similar? –  HTG Dec 29 '11 at 21:27
1  
I think the problem is that each chapter file contains a \chapter*, which doesn't include \stepcounter or \refstepcounter, making all chapters have the initialized chapter counter value of 0. –  Werner Dec 29 '11 at 21:35
    
Yup, that worked. I added a \refstepcounter{chapter} to the beginning of each chapter file. Works like a dream! –  HTG Dec 29 '11 at 21:36
    
Ok, I'll give that a try. Thanks! –  HTG Dec 29 '11 at 21:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.