# latex text manipulation of \ref ouput

For some reason I need to manipulate the output of the \ref command to match the patter of four digits without dots. For example:

\title{A Very Simple \LaTeXe{} Template}
\documentclass[12pt]{article}

\begin{document}
\section{Section 1}\label{sec:section1}
\subsection{Section 1.1}\label{sec:section11}

\ref{sec:section11}

\end{document}


The output of \ref{sec:section11} is "1.1" but I need it to "be 0011".

I get some code working using xparse and l3regex I found here: How to replace text, but this only works with plain text.

 \replace{1.1}{.}{} results in 11
\replace{\ref{sec:section11}}{.}{} results in 1.1

• Hi, did you look at the hyperref package documentation ? – BambOo Apr 4 '18 at 17:13
• Welcome to TeX.SX! What would be the general rule? – egreg Apr 4 '18 at 22:36

If you want to perform string substitutions it is better to use \getrefnumber from the refcount package instead of \ref. It is an expandable version of \ref that can be used for things like string substitutions. Some of the answers to the question you linked to still might not work out of the box with \getrefnumber though if they do not first expand their arguments (and thus perform substitutions on \getrefnumber{sec:section11} instead of the on 1.1).

Here is a possible solution that uses the xstring package. I don't know precisely by what procedure 1.1 is supposed to be transformed into 0011, so this probably doesn't precisely do what you want.

\documentclass{article}

\usepackage{refcount}
% \usepackage{hyperref} %% Optional
%% N.B. refcount is automatically loaded by hyperref, so you don't need both

\usepackage{xstring} %% Package for string manipulation

\DeclareRobustCommand*\myref[1]{\formatsectionnumber{\getrefnumber{#1}}}
\newcommand*\formatsectionnumber[1]{%
\begingroup
%% Prepend and append two 0's, perform the substitution and store the result in \temp:
\StrSubstitute[0]{00#100}{.}{}[\temp]%
%% Store the first four characters in \temp:
\StrLeft{\temp}{4}[\temp]%
%% Print the result
\temp
\endgroup
}

\begin{document}

\section{Section 1}\label{sec:section1}

\subsection{Section 1.1}\label{sec:section11}

Section~\myref{sec:section1} has a subsection which we will call \myref{sec:section11}.

\end{document}


The output of \ref is a sequence of commands for printing the number. You want an expandable version, which is provided by refcount.

With the help of expl3 it's not difficult to get what you want (but the precise rule is mysterious).

\documentclass{article}

\usepackage{refcount}
\usepackage{xparse}

\ExplSyntaxOn
% define an expl3 version
\cs_new_eq:NN \christian_getrefnumber:n \getrefnumber
% allocate a tl variable
\tl_new:N \l__christian_bizarreref_tl
% the main command
\NewDocumentCommand{\bizarreref}{m}
{
% get the reference number and store it
\tl_set:Nx \l__christian_bizarreref_tl { \christian_getrefnumber:n { #1 } }
% remove all periods
\tl_replace_all:Nnn \l__christian_bizarreref_tl { . } { }
\prg_replicate:nn { 4 - \tl_count:N \l__christian_bizarreref_tl } { 0 }
% the reference
\tl_use:N \l__christian_bizarreref_tl
}
\ExplSyntaxOff

\begin{document}
\section{Section 1}\label{sec:section1}
\subsection{Section 1.1}\label{sec:section11}

\bizarreref{sec:section11}

\end{document}