# Include the subsection number in cross-reference to a theorem-like environment

I think it is better post my code and then ask my question

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{lipsum}
\usepackage[T1]{fontenc} % optional
\usepackage{thmtools}

\declaretheoremstyle[%
spaceabove=\topsep,
spacebelow=\topsep,
bodyfont=\itshape\small,
]{remark}
\declaretheorem[style=remark,numberwithin=subsection]{remark}
\renewcommand{\theremark}{\arabic{remark}}
\newcommand\rmk[1]{\textit{Remark} \textup{\ref{#1}}}

\makeatletter
\renewcommand*{\p@section}{\S} %Add section symbol to section reference
\renewcommand*{\p@subsection}{\P} %Add section symbol to section reference
\makeatother

\begin{document}

\section{A}
\lipsum[1]

\subsection{A.1}
\label{a}

\begin{remark}
\label{remark}
\lipsum[2]
\end{remark}

\lipsum[2]

\subsection{A.2}

\begin{remark}
\lipsum[2]
\end{remark}

\lipsum[3]

\section{B}
\lipsum[1]
\subsection{B.1}

As we can see in \ref{a}~\rmk{remark}...

\end{document}


As you can see in my code, remark enumeration (and other environment enumerations) are restarted at each subsection. I'm very happy with that, but the problem is that when I want to cross-reference a remark from another subsection, I must specify the subsection explicitly, by writing \ref{sec:stuff}.

Is there a modification of my command \rmk such that includes the subsection reference automatically? Maybe create a new command which tells you the current subsection of that environment?

• In my point it is easier with zref – user31729 Mar 8 '18 at 21:17
• I had heard about cleveref package (I've even used it) but not about zref. I'll looking at it on the internet. Thanks. – Dog_69 Mar 8 '18 at 21:55

You can define \rmk in such a way that the number of the subsection is printed only if the referenced remark is in another subsection.

\documentclass[a4paper,12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{xparse}

\usepackage{lipsum}

\declaretheoremstyle[
spaceabove=\topsep,
spacebelow=\topsep,
bodyfont=\normalfont\small,
]{remark}
\declaretheorem[style=remark,numberwithin=subsection]{remark}
\renewcommand{\theremark}{\arabic{remark}}

\makeatletter
\renewcommand*{\p@section}{\S} %Add section symbol to section reference
\renewcommand*{\p@subsection}{\P} %Add section symbol to section reference
\renewcommand*{\p@remark}{\perhapssubsection{\thesubsection}}
\makeatother

\newcommand{\rmk}[1]{\textit{Remark}~\textup{\ref{#1}}}

\ExplSyntaxOn
\NewDocumentCommand{\perhapssubsection}{m}
{
\str_if_eq_x:nnF { #1 } { \thesubsection } { #1. }
}
\ExplSyntaxOff

\begin{document}

\section{A}
\lipsum[1]

\subsection{A.1}
\label{a}

\begin{remark}
\label{remark}
\lipsum[2]
\end{remark}

\lipsum[2]

\subsection{A.2}

\begin{remark}
\lipsum[2]
\end{remark}

\lipsum[3]

\section{B}
\lipsum[1]
\subsection{B.1}

\begin{remark}\label{remarkhere}
\lipsum[2]
\end{remark}

As we can see in \rmk{remark}
and in \rmk{remarkhere}

\end{document}


I changed a bit the appearance of the remarks: I believe that the head should be in italics, but the body should be upright. Not only for uniformity with the references, but also for not making remarks too prominent: you're typesetting them in smaller type, after all. Feel free to revert.

• your answer adding ''printed only if the referenced remark is in another subsection'' is awesome. In fact, this is was I wanted but I felt badly asking for so many things. – Dog_69 Mar 8 '18 at 21:59
• @Dog_69 I just polished my crystal ball. 😀 – egreg Mar 8 '18 at 22:00
• Yes, because you are a genius. All of you are genius in fact: ChristianHupfer, Mico, Ulrike Fischer, samcarter, barbara beeton... and many others I'm forgetting or simply I don't know them. – Dog_69 Mar 8 '18 at 22:05
• It's too much but.. Would it be possible to change the order to able to get 1.1 Remark 1 instead of Remark 1.1.1? – Dog_69 Mar 9 '18 at 22:34
• Forget it. I have found a way to do it. But thanks. – Dog_69 Mar 9 '18 at 22:36

Mostly, you need to add the instruction \renewcommand*{\p@remark}{\thesubsection.} in the preamble.

You may also want to look into loading the cleveref package (and its \cref macro) to simplify and streamline the creation of more than one cross-reference at a time. See the final paragraph of the following screenshot for an example of \cref.

% !TEX TS-program = pdflatex
\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage[T1]{fontenc} % optional
\usepackage{thmtools}

\declaretheoremstyle[%
spaceabove=\topsep,
spacebelow=\topsep,
bodyfont=\itshape\small,
]{remark}
\declaretheorem[style=remark,numberwithin=subsection]
{remark}
\renewcommand{\theremark}{\arabic{remark}}
\newcommand\rmk[1]{\textit{Remark}~\textup{\ref{#1}}}

\newcommand\blurb{Nam dui ligula, fringilla a, euismod sodales,
sollicitudin vel, wisi. Morbi auctor lorem non justo. Nam
lacus libero, pretium at, lobortis vitae, ultricies et, tellus.}

\makeatletter
\renewcommand*{\p@section}{\S}
\renewcommand*{\p@subsection}{\P}
\renewcommand*{\p@remark}{\thesubsection.} % <--- new
\makeatother

\usepackage{cleveref}
\crefname{remark}{\textit{Remark}}{\textit{Remarks}}

\begin{document}

\section{Bla bla bla}
\subsection{Ble ble ble}

\begin{remark} \label{remark_e}
\blurb
\end{remark}

\subsection{Bli bli bli}

\begin{remark} \label{remark_i}
\blurb
\end{remark}

\section{Blo blo blo}
\subsection{Blu blu blu}

As we can see in \rmk{remark_e} as well as in \rmk{remark_i}, \dots

\bigskip\noindent
As we can see in \cref{remark_e,remark_i}, \dots
\end{document}

• Fantastic answer. I've decided to mark @egreg's answer because it includes a powerful tool which includes only the subsetion if it is different. On the other hand, I appreciate your comments about the cleveref package. – Dog_69 Mar 8 '18 at 22:02