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.

Background

Using LyX and KOMA Script to author a technical manual.

Problem

The documents are strewn with the following references:

\ref{fig:Example-Database-Type}

When converted to PDF, this results in "1.1" for the figure number, whereas I would prefer it to read "Figure 1.1". Similarly, for Tables and Listings, they should read "Table 1.1" and "Listing 1.1", respectively.

Code

The caption code is:

\caption{\label{fig:Example-Query-Statement}Example Query Statement}

The reference code is:

\ref{fig:Example-Query-Statement}

The document preamble includes:

\usepackage{hyperref}

The result is: "The screen shot in 1.1 shows ..."

The desired result is: "The screen shot in Figure 1.1 shows ..."

Ideas

Tried:

\let\oldref=\ref
\renewcommand{\ref}[1]{Figure {\oldref{#1}}}

Tried:

\usepackage{fancyref}
\renewcommand{\ref}[1]{\fref{#1}}

Tried:

\usepackage{hyperref}

Question

How do you change the \ref command to include a \figurename prefix without having to edit (and thus duplicate) the code for every figure (that already has the \ref)?

Thank you!

share|improve this question
1  
see also: tex.stackexchange.com/questions/5797/… –  Caramdir Nov 27 '10 at 18:34
1  
add comment

2 Answers

up vote 4 down vote accepted

Hyperref has autoref which takes care of this stuff. It is described briefly here.

This solution doesn't do exactly what you want. That is, it doesn't redefine \ref. But I'm not sure that's a great idea anyway. (weirdly \let\ref\autoref doesn't seem to do anything). So to have what you want, you need to do two things:

Add \usepackage{hyperref} to your preamble and then put \let\ref\autoref just after your \begin{document}

If you don't like the ugly coloured borders hyperref puts on your references see this. Alternatively, you can also add the nohyperref package after the hyperref package.

Hyperref causes lots of nasty package conflicts, so put it near the end of your preamble.

share|improve this answer
    
@Seamus: A little too briefly for me; I don't understand how to change the document preamble to use it. –  Dave Jarvis Nov 27 '10 at 19:13
    
@Dave I've added some more information... –  Seamus Nov 27 '10 at 19:25
    
@Seamus: I am using LyX. LyX uses \ref by default. I do not want to search/replace the default every time I add a table, listing, or figure. :-) Thanks for the extra details, though. –  Dave Jarvis Nov 27 '10 at 19:31
    
@Dave Jarvis I've changed my answer. It works now. –  Seamus Nov 27 '10 at 21:40
    
@Seamus: Thanks for the effort; given the issues you mentioned with hyperref, I think a solution that does not rely on that package is more robust. –  Dave Jarvis Nov 28 '10 at 4:11
show 6 more comments

Solution

  1. Append the following to the preamble:
    \usepackage{cleveref}
  2. Insert the following after the document begins:
    \let\ref\Cref

If you split out all of your chapters into separate files, it will help to add the following to the start of each chapter:

\input{chapter.tex}

In this fashion, you can edit a single file and have the changes apply across all chapters.

share|improve this answer
    
This doesn't work. And nor does my solution with hyperref's autoref. –  Seamus Nov 27 '10 at 21:35
    
sorry. i didn't see that the let comes after the document begins. –  Seamus Nov 27 '10 at 23:46
add comment

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.