Abbreviated references: just the necessary references, no more
In theory this is not very difficult. You can use zref to store all the various values of section, subsection etc of a label, and then you only need to test their values and compare with the current values and decide what should happen.
In practice it can get a bit tiresome if you really want to do it for all levels as there are quite a number of tests and one normally need a large piece of paper to get all the branches right (I never do on the first attempt).
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel, blindtext}
\setcounter{secnumdepth}{5} % fünf Ebenen
\renewcommand*{\thesection}{\Alph{section}.}
\renewcommand*{\thesubsection}{\Roman{subsection}.}
\renewcommand*{\thesubsubsection}{\arabic{subsubsection}.}
\renewcommand*{\theparagraph}{\alph{paragraph})}%)
\renewcommand*{\thesubparagraph}{\alph{subparagraph}\alph{subparagraph})}
\usepackage{zref-user,zref-counter}
\usepackage{expl3}
\makeatletter
\zref@newprop{section}[1]{\the\c@section}
\zref@newprop{subsection}[1]{\the\c@subsection}
\zref@addprop{main}{section}
\zref@addprop{main}{subsection}
\ExplSyntaxOn
\newcommand\myref[1]{%
\hyperref[#1]{
\int_compare:nTF
{
\zref@extractdefault {#1} {section} {0} = \the\c@section
}
{
%ref is from current section
\int_compare:nTF
{\zref@extractdefault {#1} {subsection} {0} = 0 }
{Was~nun?}
{\int_to_Roman:n{\zref@extractdefault {#1} {subsection} {0}}.}
}
{
%ref is from other section
\int_to_Alph:n {\zref@extractdefault {#1} {section} {0}}.
\int_to_Roman:n {\zref@extractdefault {#1} {subsection} {0}}
}}}
\ExplSyntaxOff
\makeatother
%\usepackage{xcolor}
\usepackage[]{hyperref}
\newcommand\Label[1]{\zlabel{#1}\label{#1}}
\begin{document}
\section{First}
\Label{sec:first}
I'm refering to the second section and I'd like to have a full
reference like this: \myref{sec:secondofsecond}.
\section{Second}
\Label{sec:second}
\myref{sec:secondofsecond}, \myref{sec:first}, \myref{sec:second}
\subsection{Firstofsecond}
\Label{sec:firstofsecond}
Text.
\subsection{Secondofsecond}
\Label{sec:secondofsecond}
\end{document}
Are you aware of the jura package? Altho' it is quite old and unsupported, it provides the fine grade level structure you sketch (and goes even further down to levels indicated by α) β) and αα) ββ)
EDIT: Note that the document markup introduced with jura
(or with \usepackage{alphanum}
that contains the level structure) is incompatible with the usual LaTeX sectioning commands (like \section
etc.). You shall use the jura
style of sectioning using \toc
, \sub
, and \levelup
.
The answer of Ulrike Fischer showed me the way and after poring over it for a whole day I found the following solution.
Probably it is the lowest level of coding you ever will read, so be warned. If someone comes up with a better solution: very well!
But without Ulrikes generous help, it would not have been possible, thank you very much!
Some comments are in German, sorry, it is late now and I doubt that I tommorrow will understand, what I wrote today! Ah, and the various, useless "(" in comments are there to make Emacs happy, which otherwise doesn't get the "{" and "}" matched.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel, blindtext}
\setcounter{secnumdepth}{5} % fünf Ebenen
\renewcommand*{\thesection}{\Alph{section}.}
\renewcommand*{\thesubsection}{\Roman{subsection}.}
\renewcommand*{\thesubsubsection}{\arabic{subsubsection}.}
\renewcommand*{\theparagraph}{\alph{paragraph})}%)
\renewcommand*{\thesubparagraph}{\alph{subparagraph}\alph{subparagraph})}
\usepackage{zref-user, zref-counter}
\usepackage{expl3}
\makeatletter
\zref@newprop{section}[1]{\the\c@section}
\zref@newprop{subsection}[1]{\the\c@subsection}
\zref@newprop{subsubsection}[1]{\the\c@subsubsection}
\zref@newprop{paragraph}[1]{\the\c@paragraph}
\zref@newprop{subparagraph}[1]{\the\c@subparagraph}
\zref@addprop{main}{section}
\zref@addprop{main}{subsection}
\zref@addprop{main}{subsubsection}
\zref@addprop{main}{paragraph}
\zref@addprop{main}{subparagraph}
\ExplSyntaxOn
\newcommand\myref[1]{%
\hyperref[#1]{%
\int_compare:nTF
{% A.-Ebene prüfen: sind wir auf dem gleichen Zweig?
\zref@extractdefault {#1} {section} {0} = \the\c@section
}
{ % Ja, wir sind auf dem gleichen Zweig
% Existiert eine weitere Ebene?
\int_compare:nTF
{\zref@extractdefault {#1} {subsection} {0} = 0}
% Wenn subsection 0 ist, also nicht existiert, dann muss section ausgegeben
% werden.
{
% \emph{Fehlerhafter Verweis}
\int_to_Alph:n {\zref@extractdefault {#1} {section} {0}}.
}
% Wenn dagegen subsection existiert, dann geht die Prüfung weiter:
{
% OK, weitere ebene existiert.
\int_compare:nTF % I.-Ebene
{
\zref@extractdefault {#1} {subsection} {0} = \the\c@subsection
}% Sind wir auf der gleichen I.-Ebene?
{% I.-Ebene: Gleiche Ebene
% Prüfung: gibt es eine weitere Ebene?
\int_compare:nTF
{\zref@extractdefault {#1} {subsubsection} {0} = 0}
{% Wenn 0, dann keine weitere Ebene und subsection ausgeben:
\int_to_Roman:n {\zref@extractdefault {#1} {subsection} {0}}.
}
{% OK, weitere Ebene existiert
\int_compare:nTF % subsubsection-Ebene
{% 1.-Ebene prüfen
\zref@extractdefault {#1} {subsubsection} {0} = \the\c@subsubsection
}
{% 1.-Ebene ist gleich
%Prüfung: gibt es eine weitere Ebene?
\int_compare:nTF % Gibt es eine a-Ebene?
{\zref@extractdefault {#1} {paragraph} {0} = 0}
{% Wenn 0, dann keine weitere Ebene und subsubsection ausgeben:
\int_to_arabic:n { \zref@extractdefault {#1} {subsubsection} {0}}.
}
{% Paragraph existiert:
\int_compare:nTF % a-Ebene prüfen
{
\zref@extractdefault {#1} {paragraph} {0} = \the\c@paragraph
}
{% a-Ebene ist gleich
% Prüfung: aa-Ebene vorhanden?
\int_compare:nTF %
{ \zref@extractdefault {#1} {subparagraph} {0} = 0}
%
{%( damit emacs happy ist
\int_to_alph:n {\zref@extractdefault {#1} {paragraph} {0} })
}
{% aa-Ebene vorhanden
\int_compare:nTF
{\zref@extractdefault {#1} {subparagraph} {0} = \the\c@subparagraph}
{\emph{Fehlerhafter VERWEIS}}
%( make emacs happy
{\int_to_alph:n { \zref@extractdefault {#1} {subparagraph} {0} }
\int_to_alph:n { \zref@extractdefault {#1} {subparagraph} {0} })
}
}
}
{% a-Ebene ist unterschiedlich (
\int_to_alph:n{\zref@extractdefault {#1} {paragraph} {0}})
}
}
}
{% 1.-Ebene ist unterschiedlich
\int_to_arabic:n{\zref@extractdefault {#1} {subsubsection} {0}}.
}
}% 3. Klammer Prüfung, ob subsection existiert (also wenn ja)
}
{% I.-Ebene: ungleich
% Also römische Nummer ausgeben
\int_to_Roman:n{\zref@extractdefault {#1} {subsection} {0}}.%
\int_compare:nTF%existiert die nächste Ebene?
{ \zref@extractdefault {#1} {subsubsection} {0} = 0 }
{}% = 0, subsubsection nicht vorhanden
{% ungleich 0
% also arabische Zahl ausgeben
\int_to_arabic:n { \zref@extractdefault {#1} {subsubsection} {0}}.
\int_compare:nTF%existiert die nächste Ebene paragraph?
{ \zref@extractdefault {#1} {paragraph} {0} = 0 }
{}% = 0, subsubsection nicht vorhanden
{% ungleich 0 (
\int_to_alph:n { \zref@extractdefault {#1} {paragraph} {0}})
}
% subpar Ebene vorhanden?
\int_compare:nTF
{ \zref@extractdefault {#1} {subparagraph} {0} = 0}
{}% ja, also existiert subpar nicht
{ %(
\int_to_alph:n {\zref@extractdefault {#1} {subparagraph} {0}}
\int_to_alph:n {\zref@extractdefault {#1} {subparagraph} {0}})
}
}
}% 3. Klammer I.-Ebene
}% 3. Klammer Prüfung, ob I. Ebene besteht.
}% 2. Klammer A.-Ebene
{% A.-Ebene: Ungleich
%ref is from other section
\int_to_Alph:n {\zref@extractdefault {#1} {section} {0}}.
\int_compare:nTF%existiert die nächste Ebene?
{ \zref@extractdefault {#1} {subsection} {0} = 0 }
{}% ja, erfüllt, keine subsection
{% subsection existiert:
\int_to_Roman:n {\zref@extractdefault {#1} {subsection} {0}}.
\int_compare:nTF%existiert die nächste Ebene?
{ \zref@extractdefault {#1} {subsubsection} {0} = 0 }
{}% = 0, subsubsection nicht vorhanden
{% ungleich 0
\int_to_arabic:n { \zref@extractdefault {#1} {subsubsection} {0} }.
\int_compare:nTF%existiert die nächste Ebene paragraph?
{ \zref@extractdefault {#1} {paragraph} {0} = 0 }
{}% = 0, paragraph nicht vorhanden
{% ungleich 0 (
\int_to_alph:n { \zref@extractdefault {#1} {paragraph} {0} })
\int_compare:nTF
{ \zref@extractdefault {#1} {subparagraph} {0} = 0}
{}% ja, also existiert subpar nicht
{% (
\int_to_alph:n {\zref@extractdefault {#1} {subparagraph} {0}}
\int_to_alph:n {\zref@extractdefault {#1} {subparagraph} {0}})
}
}
}
}
}% 3. Klammer A.-Ebene
}% hyperref Klammer zu
}%Ende Befehlsdefinition myref
\ExplSyntaxOff
\makeatother
\usepackage[]{hyperref}
\newcommand\Label[1]{\zlabel{#1}\label{#1}}
\begin{document}
\textbf{Präambel}
\begin{enumerate}
\item Reference to first section: \myref{sec:first}
\item Reference to second section: \myref{sec:second}
\item Reference to snd section / first subsection: \myref{sec:firstofsecond}
\item Ref. to snd sect. / second subsect: \myref{sec:secondofsecond}
\item Ref to snd sect. / second subsect / first arabic:
\myref{CLA:first-second.second}
\item Ref. to snd sect. / second subsect / first arabic / second par:
\myref{CLA:second-paragr}
\item Subparagraph: \myref{CLA:first-sub}
\end{enumerate}
\section{First}
\Label{sec:first}
Sytematically:
\begin{enumerate}
\item Reference to this section should display a warning: \myref{sec:first}.
\item Reference to next section: \myref{sec:second}
\item Reference to next section / first subsection: \myref{sec:firstofsecond}
\item Ref. to next sect. / second subsect: \myref{sec:secondofsecond}
\item Ref to next sect. / second subsect / first arabic:
\myref{CLA:first-second.second}
\item Ref. to next sect. / second subsect / first arabic / second par:
\myref{CLA:second-paragr}
\item Subparagraph: \myref{CLA:first-sub}
\end{enumerate}
\section{Second}
\Label{sec:second}
Sytematically:
\begin{enumerate}
\item Reference to previous section: \myref{sec:first}.
\item Reference to this section should display a warning: \myref{sec:second}
\item Reference to this section / first subsection: \myref{sec:firstofsecond}
\item Ref. to this sect. / second subsect: \myref{sec:secondofsecond}
\item Ref to this sect. / second subsect / first arabic:
\myref{CLA:first-second.second}
\item Ref. to this sect. / second subsect / first arabic / second par:
\myref{CLA:second-paragr}
\item Subparagraph: \myref{CLA:first-sub}
\end{enumerate}
\subsection{Firstofsecond}
\Label{sec:firstofsecond}
Sytematically:
\begin{enumerate}
\item Reference to previous section: \myref{sec:first}.
\item Reference to this section: \myref{sec:second}
\item Reference to this section / first subsection: \myref{sec:firstofsecond}
\item Ref. to this sect. / second subsect: \myref{sec:secondofsecond}
\item Ref to this sect. / second subsect / first arabic:
\myref{CLA:first-second.second}
\item Ref. to this sect. / second subsect / first arabic / second par:
\myref{CLA:second-paragr}
\item Subparagraph: \myref{CLA:first-sub}
\end{enumerate}
\subsection{Secondofsecond}
\Label{sec:secondofsecond}
Now down the tree:
\subsubsection{first of second.second}
\Label{CLA:first-second.second}
Just some letters.
\paragraph{first-paragraph}
\Label{CLA:first-paragraph}
And more text. Ref to next paragraph, should be a b): \myref{CLA:second-paragr}
\begin{enumerate}
\item Reference to previous section: \myref{sec:first}.
\item Reference to this section: \myref{sec:second}
\item Reference to this section / first subsection: \myref{sec:firstofsecond}
\item Ref. to this sect. / second subsect: \myref{sec:secondofsecond}
\item Ref to this sect. / second subsect / first arabic:
\myref{CLA:first-second.second}
\item Ref. to this sect. / second subsect / first arabic / second par:
\myref{CLA:second-paragr}
\item Subparagraph: \myref{CLA:first-sub}
\end{enumerate}
\paragraph{second paragr}
\Label{CLA:second-paragr}
\begin{enumerate}
\item Reference to previous section: \myref{sec:first}.
\item Reference to this section: \myref{sec:second}
\item Reference to this section / first subsection: \myref{sec:firstofsecond}
\item Ref. to this sect. / second subsect: \myref{sec:secondofsecond}
\item Ref to this sect. / second subsect / first arabic:
\myref{CLA:first-second.second}
\item Ref. to this sect. / second subsect / first arabic / second par:
\myref{CLA:second-paragr}
\end{enumerate}
\subparagraph{first sub}
\Label{CLA:first-sub}
\begin{enumerate}
\item Reference to previous section: \myref{sec:first}.
\item Reference to this section: \myref{sec:second}
\item Reference to this section / first subsection: \myref{sec:firstofsecond}
\item Ref. to this sect. / second subsect: \myref{sec:secondofsecond}
\item Ref to this sect. / second subsect / first arabic:
\myref{CLA:first-second.second}
\item Ref. to this sect. / second subsect / first arabic / second par:
\myref{CLA:second-paragr}
\item Subparagraph: \myref{CLA:first-sub}
\end{enumerate}
dsfsdf
\subsubsection{Last one}
\Label{CLA:last-one}
text
\paragraph{paragraph somewhere}
\Label{CLA:paragraph-somewhere}
OK, the par above should display a). And now we refer to the next subpar:
\myref{CLA:second-subpar}.
And now a subpar:
\subparagraph{second subpar}
\Label{CLA:second-subpar}
text
\subparagraph{third subpar}
\end{document}