[Tex/LaTex] References with section and subsection name

cross-referencingsectioning

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.

Best Answer

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