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 declare sections and subsections as such in my document:

\section{1}
\label{sec:1}

\subsection{a}
\label{sec:1a}

These are formatted using the titlesec package to display "Problem 1" and "Part a" respectively.

When I reference a given subsection using the \ref{label} command, it displays something like 1.1, which are clearly the values of the section/subsection counters. What I want however, is to display something like 1a in references, where the1 and the a respectively take their values from the section/subsection names. I have tried using the nameref package to do this, but it only displays the a part, and there doesn't seem like a way to have the name of the section (containing the subsection) printed as well.

Any suggestions are welcome -- either in raw LaTeX, modifying \nameref, or using some other package.

share|improve this question
    
This previous answer might be able to be adapter, incidentally: tex.stackexchange.com/questions/62241/… –  Noldorin Dec 15 '12 at 18:21
    
If you name your \sections and \subsections exactly like this, then using \renewcommand{\thesubsection}{\thesection\alph{subsection}} would yield 1a for \ref{sec:1a}. However, since you're using a numbering scheme inside titles, it's best to use something else entirely that automates this. Otherwise you'll have to do a lot of work if you move stuff around. –  Werner Dec 15 '12 at 18:32
    
@Werner: Indeed, I'd luck something a little more generic. Even if I decide to use that exact naming convention, the counters and titles could get out of sync too easily. Thanks for the tip though! –  Noldorin Dec 15 '12 at 18:35
1  
Before answering, I have to say i think this is a really bad idea. If you don't want section titles, use \section{} etc. and configure the numbering scheme on the secion number side. –  Stephan Lehmke Dec 15 '12 at 20:27
    
@StephanLehmke: I want the format "Problem 1/2/3" though, for titles of sections. And likewise "Part A/B/C" for subsections. –  Noldorin Dec 15 '12 at 20:49
add comment

1 Answer 1

up vote 9 down vote accepted

With my comment in mind, here's a patch for \nameref which will combine the names of two sectioning levels:

\documentclass{article}

\usepackage{nameref}

\makeatletter
\def\NR@gettitle#1{%
  \GetTitleString{#1}%
  \expandafter\let\csname @currentlabelname@\thesection@level\endcsname\GetTitleStringResult
  \edef\@currentlabelname
  {%
    \ifcsname
      @currentlabelname@\number\numexpr\c@section@level-\@ne\relax
    \endcsname
      \unexpanded\expandafter\expandafter\expandafter
      {%
        \csname 
          @currentlabelname@\number\numexpr\c@section@level-\@ne\relax
        \endcsname
      }%
    \fi
    \unexpanded\expandafter{\GetTitleStringResult}%
  }
}
\makeatother


\begin{document}
\section{1}
\label{sec:1}

\subsection{a}
\label{sec:1a}

section: \nameref{sec:1}.

subsection: \nameref{sec:1a}.
\end{document}

output

I hope it is clear that this is not very general, but it answers your exact question. It sould be possible to generalize it further.

Edit

Patch when titlesec is used...

\usepackage{etoolbox}

\makeatletter
\def\chapter@level{0}
\def\section@level{1}
\def\subsection@level{2}
\def\subsubsection@level{3}
\def\paragraph@level{4}
\def\subparagraph@level{5}

\patchcmd\ttl@straight@i{\def\@currentlabelname{#2}}
{%
  \expandafter\def
  \csname @currentlabelname@\csname#1@level\endcsname\endcsname{#2}%
  \edef\@currentlabelname
  {%
    \ifcsname
      @currentlabelname@\number\numexpr\csname#1@level\endcsname-\@ne\relax
    \endcsname
      \unexpanded\expandafter\expandafter\expandafter
      {%
        \csname 
          @currentlabelname@\number\numexpr\csname#1@level\endcsname-\@ne\relax
        \endcsname
      }%
    \fi
    \unexpanded{#2}%
  }
}{}{}
\makeatother
share|improve this answer
    
How odd. That still only prints a for me. :S –  Noldorin Dec 15 '12 at 21:20
    
@Noldorin Remember to make two runs. –  Stephan Lehmke Dec 15 '12 at 21:21
    
Yep, issue persists I'm afraid! –  Noldorin Dec 15 '12 at 21:21
    
@Noldorin Interesting. Are you using the same document I posted, without alteration? –  Stephan Lehmke Dec 15 '12 at 21:22
    
Nope it's in my own document... although I can't figure out why that should be any different. :/ –  Noldorin Dec 15 '12 at 21:23
show 4 more comments

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.