Text FormattingProblems with appendicies and \ref{}

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
irr
Posts: 5
Joined: Thu Jun 11, 2009 6:30 am

Problems with appendicies and \ref{}

Post by irr »

I am using the report class for my dissertation with two appendicies and hyperref for clickable pdf links, each included appendix file looks like this...(where A replaces B in the first appendix)

Code: Select all


\clearpage
\phantomsection
\addcontentsline{toc}{chapter}{Appendix B}%text appears in toc

\renewcommand{\theequation}{B.\arabic{equation}}%orB\...
\setcounter{equation}{0} % reset counter
\renewcommand{\thesection}{B.\arabic{section}}
\setcounter{section}{0}
\renewcommand{\thesubsection}{B.\arabic{section}.\arabic{subsection}}
\setcounter{subsection}{0}
\chapter*{Appendix B}
\section{Name}
\label{appB}
Text...
The problem is that when I use \ref{appA} it points to Appendix B instead, probably because of my renewcommands above. How should I do this so I get correct hyperlinks, AND proper subsection numbering.

Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Problems with appendicies and \ref{}

Post by localghost »

I wonder why to take this detour to get your appendices. A simple switch will do much better.

Code: Select all

\appendix
\chapter{Title of the Appendix}\label{chp:appA}
This will also lead to correct references and accordingly numbered sections and equations. Perhaps the appendix package is useful for you.


Best regards and welcome to the board
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
irr
Posts: 5
Joined: Thu Jun 11, 2009 6:30 am

Problems with appendicies and \ref{}

Post by irr »

I still have similar problems this way...

I use

Code: Select all

\clearpage
\phantomsection
\addcontentsline{toc}{chapter}{Appendix A}%text appears in toc
\appendix
\chapter*{Appendix A}\label{appA}
\renewcommand{\theequation}{A.\arabic{equation}}
\setcounter{equation}{0} % reset counter 
\renewcommand{\thesection}{A.\arabic{section}}
and

Code: Select all

\clearpage
\phantomsection
\addcontentsline{toc}{chapter}{Appendix B}%text appears in toc
%\appendix
\chapter*{Appendix B}\label{appB}
\renewcommand{\theequation}{B.\arabic{equation}}
\setcounter{equation}{0} % reset counter
\renewcommand{\thesection}{B.\arabic{section}}
\setcounter{section}{0}
(Note that I am using \chapter* because I don't wish to see ``Chapter'' in the appendix title, and this must be breaking the following \label command?

Also, with these commands, \ref pointing to section A.1 actually likes to B.1. I am still forced to redefine the counters in appendix B so that my first section does not start with B.2 (I want B.1).
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Problems with appendicies and \ref{}

Post by localghost »

irr wrote:[...] Note that I am using \chapter* because I don't wish to see ``Chapter'' in the appendix title, and this must be breaking the following \label command? [...]
With the \appendix switch the chapter name is changed to "Appendix". You could have found that out by trying the suggested lines.

Another possible solutions for standard classes is to modify the appearance of chapter by means of the titlesec package. Consider the following example.

Code: Select all

\documentclass[11pt,a4paper,english]{report}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{babel}
\usepackage[includeheadfoot,margin=2cm]{geometry}
\usepackage[pagestyles,raggedright,bf,sf]{titlesec}
\usepackage{blindtext}

\parindent0em

% Layout setup (titlesec)
\titleformat{\chapter}{\normalfont\Huge\bfseries}{\thechapter}{20pt}{\Huge}
\newpagestyle{main}{%
	\headrule
	\footrule
	\sethead[\thepage][][\bfseries\sffamily\thesection\quad\sectiontitle]{\bfseries\sffamily\chaptername~\thechapter\quad\chaptertitle}{}{\thepage}
	\setfoot[][][\scshape Institution]{Title of the document}{}{}
}
\newpagestyle{appendix}{%
	\headrule
	\footrule
	\sethead[\thepage][][\bfseries\sffamily\thechapter\quad\chaptertitle]{\bfseries\sffamily\thechapter\quad\chaptertitle}{}{\thepage}
	\setfoot[][][\scshape Institution]{Title of the document}{}{}
}

\begin{document}
  \pagestyle{main}
  \blinddocument
  \appendix
  \pagestyle{appendix}
  \blinddocument
\end{document}
This way you can use structuring commands as usual and don't have to modify several counters.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
irr
Posts: 5
Joined: Thu Jun 11, 2009 6:30 am

Problems with appendicies and \ref{}

Post by irr »

Thanks for all your help localghost, it looks like this will solve my appendix problem, however, the \newpagestyle stuff seems to have run into a little snag with my fancyhdr...

I have switched to the nccfancyhdr package, but still can't my page numbers to be in the upper left corner. My old code with fancyhdr was:

Code: Select all

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} %delete the current section for header and footer
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0.0pt}
\fancypagestyle{plain}{} 
And my new code (page numbers bottom center, not top right where I want them) is:

Code: Select all

\documentclass[12pt,letterpaper]{report}
\usepackage[plain]{nccfancyhdr}
\usepackage[compact]{titlesec}
\usepackage{sectsty}
\sectionfont{\normalsize}%change section font size from \Large to \normalsize
\subsectionfont{\normalsize}
\subsubsectionfont{\normalsize}

\usepackage[total={6.0in,8.625in},
top=1.25in, left=1.5in, headsep=0.25in, headheight=14.5pt]{geometry}

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

\setlength{\emergencystretch}{5pt}%5pt DO NOT LET WORDS GO INTO MARGIN!!!
\usepackage{setspace}
\usepackage[sort&compress]{natbib} %this stuff
\bibliographystyle{unsrt}
\renewcommand\bibsection{{\noindent\normalfont\bfseries\Large References}} 
\newcommand\bibpreamble{\vspace{1.03cm}}

\usepackage{latexsym}%11 extra symbols
\usepackage{rotating}%for sideways tables
\usepackage{times}
%\usepackage[garamond]{mathdesign}
\usepackage{amsmath,bm}
\usepackage{mathbbol}

\usepackage{verbatim}%needed for verbatim environment used in appendicies
\usepackage{moreverb}%needed for listing environment used in appendicies, same as verbatim but with line #s

\pagestyle{fancy}

\titleformat{\chapter}{\normalfont\bfseries\Large}{Chapter \thechapter.}{.7em}{}
%\titleformat{\chapter}{\normalfont\Huge\bfseries}{\thechapter}{20pt}{\Huge}
\newpagestyle{main}{%
\fancyhf{} %delete the current section for header and footer
\fancyhead[r]{\thepage}
\renewcommand{\headrulewidth}{0.pt}
%\fancypagestyle{plain}{}
}
\newpagestyle{appendix}{%
\titleformat{\chapter}{\normalfont\bfseries\Large}{Appendix \thechapter:}{.7em}{}
}

\usepackage{color}
\definecolor{custom}{rgb}{1, 0, 0}%{.290, .463, .431}
\usepackage[
   pdftitle={},
   pdfauthor={},
   pdfkeywords={},
   dvips]{hyperref}
\usepackage[all]{hypcap}%this line makes hyperref point to the top of figs and tables
\hypersetup{
letterpaper=true,
colorlinks=TRUE,
linkcolor=custom,
anchorcolor=custom,
filecolor=custom,
urlcolor=custom,
citecolor=custom,
bookmarksnumbered=true,
pdfhighlight=/N
}

\begin{document}
  \pagestyle{main}
  \doublespacing
%  \pagenumbering{roman}
\pagenumbering{arabic}
\chapter{Title}\label{chap:1}
Hello there, see Appendix~\ref{app:A}, see Apendix~\ref{app:B}
\newpage
  \appendix
  \pagestyle{appendix}
  \chapter{Title}\label{app:A}
  Appendix text here, see Chapter~\ref{chap:1}, Appendix~\ref{app:B}.
  \newpage
  \pagestyle{appendix}
  \chapter{Title}\label{app:B}
  Another appendix
\end{document}
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Problems with appendicies and \ref{}

Post by localghost »

irr wrote:Thanks for all your help localghost, it looks like this will solve my appendix problem, however, the \newpagestyle stuff seems to have run into a little snag with my fancyhdr [...]
It goes with out saying that you can't mix the the two packages. Otherwise this will cause a clash.
irr wrote:[...]I have switched to the nccfancyhdr package, but still can't my page numbers to be in the upper left corner. My old code with fancyhdr was:

Code: Select all

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} %delete the current section for header and footer
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0.0pt}
\fancypagestyle{plain}{} 
There is a contradiction. This code produces the page number in the upper right corner and leaves all other areas blank.
irr wrote:And my new code (page numbers bottom center, not top right where I want them) is [...]
That's irritating once more. Nevertheless I worked out a basic structure which should match your ideas.

Code: Select all

\documentclass[12pt,letterpaper,english]{report}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{babel}
\usepackage[total={6.0in,8.625in},top=1.25in,left=1.5in,headsep=0.25in, headheight=14.5pt]{geometry}
\usepackage[doublespacing]{setspace}  % Double interline spacing
\usepackage[compact,pagestyles]{titlesec}
\usepackage{mathptmx}   % times style for text and math
\usepackage[scaled]{helvet}  % Helvetica as sans serif font
\usepackage{courier}  % Mono spaced typewriter font
\usepackage[svgnames]{xcolor}  % Advanced color management
\usepackage{listings}  % Include programming code
\usepackage{blindtext}

\emergencystretch5pt
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

% Layout setup (titlesec)
\titleformat{\chapter}{\normalfont\bfseries\Large}{\chaptername\ \thechapter.}{.7em}{}
\titleformat{\section}{\normalfont\normalsize\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}{\normalfont\normalsize\bfseries}{\thesubsection}{1em}{}
\titleformat{\subsubsection}{\normalfont\normalsize\bfseries}{\thesubsubsection}{1em}{}

\newpagestyle{main}{%
  \sethead[\thepage][][]{}{}{\thepage}
  \setfoot[][][]{}{}{}
}

\definecolor{custom}{rgb}{1, 0, 0}%{.290, .463, .431}
\usepackage[
  pdftitle={},
  pdfauthor={},
  pdfkeywords={},
]{hyperref}
\usepackage[all]{hypcap}  % this line makes hyperref point to the top of figs and tables

\hypersetup{
  letterpaper=true,
  colorlinks=TRUE,
  linkcolor=custom,
  anchorcolor=custom,
  filecolor=custom,
  urlcolor=custom,
  citecolor=custom,
  bookmarksnumbered=true,
  pdfhighlight=/N
}

\begin{document}
  \pagestyle{main}
  \blinddocument
  \appendix
  \blinddocument
  \blinddocument
\end{document}
The following packages are not needed any more.
For more information take a close look at the manuals which can be found on CTAN.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
irr
Posts: 5
Joined: Thu Jun 11, 2009 6:30 am

Re: Problems with appendicies and \ref{}

Post by irr »

Ok, that's all cool, but the last problem I am having is that those header/footer commands do not affect the first page of the toc, lof, lot, and chapters. For these pages the page numbers remain at bottom center, and I wish them to ALL be in top right...
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Problems with appendicies and \ref{}

Post by localghost »

At first you should take note of the fact that chapter pages and similar ones intentionally differ in their page style. As already mentioned a close look at Section 5 (Page styles, p. 13ff) would have shown you that there is only a simple modification for the plain page style needed.

Code: Select all

\renewpagestyle{plain}{%
  \sethead[\thepage][][]{}{}{\thepage}
  \setfoot[][][]{}{}{}
}
Add these lines to your preamble and you get the desired result. Problems related to packages (and classes) are often solved by studying their manuals. That's what these manuals are made for.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
irr
Posts: 5
Joined: Thu Jun 11, 2009 6:30 am

Re: Problems with appendicies and \ref{}

Post by irr »

Thanks, localghost, this works great! I will now stop bothering you and read the manuals for the rest of my latex woes... :D
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Problems with appendicies and \ref{}

Post by localghost »

irr wrote:[...] I will now stop bothering you and read the manuals for the rest of my latex woes [...]
I'm not bothered by questions. I like solving problems (as far as I'm able to). But relatively small issues are better solved by taking a look at the according manual. Packages authors like the thought that somebody reads their manuals for correct handling of their work.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
Post Reply