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'm looking for a LaTeX command, say \foo, which expands to the type of section I'm in. In the following, the first \foo should expand to "Section", the second to "Subsection", and the third one to "Figure":

\section{aaa}
Hello \foo

\subsection{bbb}
Hello \foo

\begin{figure}
  whatever
  \caption{bla \foo}
\end{figure}

I'd like to have this to build a labeling package which knows where the label comes from, i.e., a \label{R4wmVP} inside a figure would make \ref{R4wmVP} print "Figure \thefigure". The benefit is that after making a section a subsection (or chapter), all references change automatically.

share|improve this question
add comment

1 Answer

You can use the package hyperref with the commands autoref and nameref

\documentclass{article}
\usepackage{hyperref}
\begin{document}
\section{aaa}\label{sec:1}
Hello \autoref{sec:1}\qquad\nameref{sec:1}

\subsection{bbb}\label{subsec:1}
Hello \autoref{subsec:1}\qquad\nameref{subsec:1}

\begin{figure}[!ht]
 whatever
 \caption{bla}\label{fig:1}
 \end{figure}

 \autoref{fig:1}\qquad\nameref{fig:1}

\end{document}

The code above produce the following result:

In relation to the comments here an example without hyperref by using nameref and cleveref

\documentclass{article}
\usepackage{nameref}
\usepackage{cleveref}

\begin{document}
\section{aaa}\label{sec:1}
Hello \nameref{sec:1}\qquad\cref{sec:1}\qquad\namecref{sec:1}

\subsection{bbb}\label{subsec:1}
Hello \nameref{subsec:1}\qquad\cref{subsec:1}\qquad\namecref{subsec:1}

\begin{figure}[!ht]
 whatever
 \caption{bla}\label{fig:1}
 \end{figure}

\nameref{fig:1}\qquad\cref{fig:1}\qquad\namecref{fig:1}
\end{document}
share|improve this answer
3  
If you don't want to use hyperref, just use the nameref package directly –  Seamus Aug 12 '11 at 15:32
1  
@Seamus: I forgot this. If you are agree, I expand my post. –  Marco Daniel Aug 12 '11 at 15:35
    
yes that's fine. Go ahead. –  Seamus Aug 12 '11 at 15:36
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.