# redefining \ref command to avoid \textit

I (not me, an answer to Using section sign (§) for cross-references to sections) have redefined the command \ref to be able to get the symbol \S:

\makeatletter
\renewcommand*{\p@section}{\SectionSymbol}% Add section symbol to section reference
\makeatother

The problem comes when I'm in an italic environment (like a theorem). Then, the numbers given by \ref{whatever} are italic. How can I solve it (avoiding use cleveref package if it is possible)?

MWE

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{lipsum}

\newtheorem{remark}{Remark}

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

\begin{document}

\section{A}

\lipsum[1]

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

\lipsum[2]

\subsection{A.2}

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

\end{document}
• What did cleveref do to you? ;o) – Bernard Dec 23 '17 at 21:26

Changing \ref is not really recommended. Change \p@section and \p@subsection rather, by gobbling \thesection and \thesubsection first and switching back to \normalfont within a group.

Of course, this will be tricky if there are other references to be done.

This is similar to my answer I provided here!

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{lipsum}

\newtheorem{remark}{Remark}

\makeatletter
\def\gobblesomething#1\csname thesection\endcsname{\begingroup\normalfont\S\thesection\endgroup}
\def\gobblesomethingother#1\csname thesubsection\endcsname{\begingroup\normalfont\P\thesubsection\endgroup}
\renewcommand{\p@section}{\gobblesomething}
\renewcommand{\p@subsection}{\gobblesomethingother}

\makeatother

\begin{document}

\section{A}

\lipsum[1]

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

\lipsum[2]

\subsection{A.2}

\begin{remark} \label{b}
See \ref{a} \lipsum[2]
\end{remark}

And what about \ref{a} or \ref{b}?
\end{document}

A similar question is this one: Reference appendix objects within texts as 'A. 1', but not 'Appendix A. 1'

It makes sense to redefine the internal \@setref to always choose upshape:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{etoolbox}

\usepackage{lipsum}

\newtheorem{remark}{Remark}

\makeatletter
\patchcmd{\@setref}{\expandafter#2#1}{\textup{\expandafter#2#1}}{}{}
\renewcommand*{\p@section}{\S}% Add section symbol to section reference
\renewcommand*{\p@subsection}{\P}
\makeatother

\begin{document}

\section{A}\label{b}

\lipsum[1]

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

\lipsum[2]

\subsection{A.2}

\begin{remark}
See \ref{a} and \ref{b} \lipsum[2]
\end{remark}

\end{document}

If hyperref is needed, the patches are a bit more complicated.

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{etoolbox}
\usepackage{hyperref}

\usepackage{lipsum}

\newtheorem{remark}{Remark}

\makeatletter
\catcode#=12
\AtBeginDocument{%
\patchcmd{\@setref}
{}{}%
}
\catcode#=6
\patchcmd{\real@setref}{\expandafter#2#1}{\textup{\expandafter#2#1}}{}{}
\renewcommand*{\p@section}{\S}% Add section symbol to section reference
\renewcommand*{\p@subsection}{\P}
\makeatother

\begin{document}

\section{A}\label{b}

\lipsum[1]

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

\lipsum[2]

\subsection{A.2}

\begin{remark}
See \ref{a} and \ref{b} \lipsum[2]
\end{remark}

\end{document}

If you only need to remove the italics from \ref you can define an \upshape version of \ref, like this:

\let\refBKP\ref
\renewcommand{\ref}[1]{{\upshape\refBKP{#1}}}

Then all your \ref commands will no longer be italicized.

• Your answer also works perfectly and I have marked it as useful. But to solve the problem with the section and subsection references I think @ChristianHupfer answer is better. I'm sorry, I had to choice one :( But thanks. – Dog_69 Dec 23 '17 at 21:41
• @Dog_69 No need to apologise ^-^. Christian's answer is far more clever. Mine is just a dirty trick ;) – Phelype Oleinik Dec 23 '17 at 21:53