Remove section number in references to theorems

How can I remove section number and subsections in references to theorems of the same, but not from various, subsection. For example, that would after such a code

\documentclass{report}

\usepackage{amsthm}
\newtheorem{theorem}{Theorem}[section]

\begin{document}
\section{sec1}
\subsection{ssec1}

\begin{theorem} \label{th:a}
Theorem text.
\end{theorem}

In theorem~\ref{th:a} ...

\subsection{ssec2}
In theorem~\ref{th:a} ...

\section{sec2}
In theorem~\ref{th:a} ...
\end{document}


get next document:

SECTION 0.1    sec1
0.1.1    ssec1

Theorem 0.1.1 Theorem text.
In theorem 1 ...

0.1.2    ssec2
In theorem 1.1 ...

SECTION 0.2   sec2
In theorem 0.1.1 ...


.

P.S. In fact, I plan to use as follows

\usepackage{thmtools}
\renewcommand*{\thetheorem}{\arabic{theorem}}


. That is, the question is rather not how to hide the numbers of sections and subsections, but how to display them

• So, if the theorem is references within the same sectional unit, the reference should exclude the sectional unit. If it's referenced outside the sectional unit, then you need to include the sectional unit. The referencing scheme seems to also be relative to the type of sectional unit, correct? That is, include only the subsection (not the section) if it's references in a different subsection. The big question is... why not stick to the original (absolute) referencing scheme? – Werner Jan 29 at 23:30

If I have understood it correctly, you would like the section number to be displayed along with the theorem number only when it is referenced from another section. Your P.S. suggests that this includes the number that is displayed in the theorem itself. (See the final comment at the bottom of this answer if you do want the section number displayed in the theorem head.)

Here is one way this could be accomplished:

\documentclass{report}

\usepackage{amsthm}
\newtheorem{theorem}{Theorem}[section]

\DeclareRobustCommand\optionalsec[1]{%
\ifnum\pdfstrcmp{#1}{\thesection}=0\else#1.\fi
}
\renewcommand\thetheorem{%
\optionalsec{\thesection}\arabic{theorem}%
}

\begin{document}

\chapter{First chapter}
\section{First section}

\begin{theorem} \label{th:a}
Theorem text.
\end{theorem}

The theorem above is called Theorem~\ref{th:a}.

\section{Second section}

The theorem in the previous section is called Theorem~\ref{th:a}.

\chapter{Second chapter}

\section{Another first section}

That theorem in the previous chapter is called Theorem~\ref{th:a}.

\end{document}


I'm defining a macro \optionalsec that takes one argument and prints it only if it is different from the current (formatted) section number. It also adds a period if this is the case. So, for instance, \optionalsec{1.1} prints nothing when called from within section 1.1, and it prints 1.1. otherwise.

I then set \thetheorem to \optionalsec{\thesection}\arabic{theorem} so that the section number is only prepended if it is referenced from another section. For this to work, the macro \optionalsec needs to be robust (see e.g. the answers to this question), which means that it will be written to the .aux file unexpanded. This is why I'm using \DeclareRobustCommand instead of \newcommand.

Remarks:

• If you're using xelatex or lualatex, or using latex to create a .dvi file, then you should instead define \optionalsec using

\usepackage{pdftexcmds} %% <- for \pdf@strcmp
\makeatletter %% <- make @ usable in command names
\DeclareRobustCommand\optionalsec[1]{%
\ifnum\pdf@strcmp{#1}{\thesection}=0\else#1.\fi
}
\makeatother  %% <- revert @


because \pdfstrcmp is only defined for pdfTeX.

• If you do want the section number to appear in the theorem head, you could define a theorem style that explicitly includes it and use that. The following theorem style matches the default, apart from the inclusion of \thesection.:

\usepackage{amsthm}
\newtheoremstyle{mytheoremstyle} %% Name
{} %% Space above
{} %% Space below
{} %% Body font
{} %% Indent amount