If you are not using hyperref
, you can use the following:
\documentclass{article}
\usepackage{enumitem}% to demonstrate compatability
\usepackage{xstring}
\renewcommand{\thesection}{\Alph{section}}% is this the default?
\makeatletter
\newcommand{\refitem}[1]% #1 = label name
{\@ifundefined{r@#1}{??}{\begingroup%
\edef\temp{\expandafter\detokenize\ref{#1}}%
\StrCut{\temp}{.}\tempsection\tempitem%
\if\thesection\tempsection\relax(\tempitem)%
\else(\tempsection.\tempitem)%
\fi
\endgroup}}
\makeatother
\begin{document}
\section{One}
\begin{enumerate}[ref=\thesection.\arabic*]
\item\label{li:one}
\end{enumerate}
\noindent I am referencing \refitem{li:one} and \refitem{li:two}.
\section{Two}
\begin{enumerate}[ref=\thesection.\arabic*]
\item\label{li:two}
\end{enumerate}
\noindent I am referencing \refitem{li:one} and \refitem{li:two}.
\end{document}

If you are using hyperref
, you will need to replace the above \refitem
with:
\usepackage{hyperref}
\makeatletter
\def\autoref#1#2#3#4#5\@nil{\edef\anchor{#3}}
\newcommand{\getrefanchor}[1]{\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\autoref
\expandafter\expandafter\expandafter\@gobble
\csname r@#1\endcsname{}\@nil}
\newcommand{\refitem}[1]% #1 = label name
{\@ifundefined{r@#1}{??}{\begingroup%
\edef\temp{\expandafter\detokenize\getrefnumber{#1}}%
\StrCut{\temp}{.}\tempsection\tempitem%
\getrefanchor{#1}% saves as \anchor
\if\thesection\tempsection\relax\hyperlink{\anchor}{(\tempitem)}%
\else\hyperlink{\anchor}{(\tempsection.\tempitem)}%
\fi
\endgroup}}
\makeatother
enumitem
? Does this only hold for lists inside sections?\labelformat
?hyperref
or any other cross-referencing package? This is important to know, as they tend to modify\ref
and may require special handling to accommodate your request.