7

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}
  • 2
    What did cleveref do to you? ;o) – Bernard Dec 23 '17 at 21:26
6

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'

| improve this answer | |
5

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}

enter image description here

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}
    {\expandafter\Hy@setref@link#1\@empty\@empty\@nil{#2}}
    {\textup{\expandafter\Hy@setref@link#1\@empty\@empty\@nil{#2}}}
    {}{}%
}
\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}
| improve this answer | |
4

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.

| improve this answer | |
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.