My document contains parts and sections. When I refer, for instance, to the section I.1, I would like \ref to output 1 when it is located in part I, and I.1 otherwise. Thus, I would like my document to look like :
I. First part
1 First section
2 Second section
Reference to the first section of the first part : 1
II. Second part
1 First section
Reference to the first section of the first part : I.1
Following this link, here is what I've tried. First, I use \counterwithin* so that section is reset each time part is incremented. Then, I change \p@section to add the part number to references to sections. Finally, I define \newref, which is supposed to drop the part number when it is not needed. Hence we get :
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{chngcntr}
\usepackage{xstring}
\usepackage{refcount}
\counterwithin*{section}{part}
\makeatletter
\renewcommand\p@section{\thepart.}
\makeatother
\newcommand\newref[1]{%
\IfRefUndefinedExpandable{#1}{\refused{#1}\textbf{??}}{%
\edef\temp{\expandafter\detokenize\getrefnumber{#1}}%
\IfStrEq{\thepart}{\StrBefore\temp.}{%
\StrBehind\temp.}{%
\temp}}}
\begin{document}
\part{First part}
\section{First section}
\label{sec:fst}
\section{Second section}
Reference to the first section of the first part : \newref{sec:fst}
\part{Second part}
\section{First section}
Reference to the first section of the first part : \newref{sec:fst}
\end{document}
However, the code above fails with "! Missing { inserted", for a reason I don't understand.
So, to recap, I would greatly appreciate to :
1) Understand why my code doesn't work ;
2) Get a solution to my problem.
Many thanks in advance.

\thepartmight not contain what you suspect. – Jun 30 '15 at 18:16