6

I have defined a command (\dingbatise) that converts a string of characters into one of dingbats. It works with \ref:

\documentclass{article}
% For clickable refs:
% \usepackage{hyperref}
% For converting numbers to dingbats:
\usepackage{xstring,pifont}
\newcommand{\dingbatise}[1]{%
\edef\tualek{#1}%
\noexpandarg%
\expandafter\StrSubstitute\expandafter{\tualek}{1}{\ding{202}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{2}{\ding{203}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{3}{\ding{204}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{4}{\ding{205}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{5}{\ding{206}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{6}{\ding{207}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{7}{\ding{208}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{8}{\ding{209}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{9}{\ding{210}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{0}{\ding{74}}[\tualek]%
\tualek
}
\begin{document}
\section{Etiam At Risus}\label{etiam}
In \dingbatise{120} pellentesque faucibus vestibulum \dingbatise{\ref{etiam}}.
\end{document}

While using the hyperref package, I can't dingbatise a particular \ref like the above anymore. How to work around this? (I don't mind if a dingbatised ref is no longer a hyperlink.)

  • The \edef\tualek{#1}% means that the file fails even without hyperref, if I copy the posted code I get ! Missing control sequence inserted. <inserted text> \inaccessible – David Carlisle Jun 13 '16 at 13:20
  • @DavidCarlisle You're using Plain TeX? – Taiki Jun 13 '16 at 13:24
  • No, of course not:-) (otherwise you'd get undefined control sequence error on \documentclass) copy the above code to a new file and process with (pdf)latex and you will get the \inaccessible error. – David Carlisle Jun 13 '16 at 13:25
  • @DavidCarlisle Oh no. This issue is interesting in its own right. The code works in Texpad, but not TeXShop. Hmmm... – Taiki Jun 13 '16 at 13:33
  • texpad and texshop are just editors used to write the input file, the error comes from latex and will be generated in either case on a new file. – David Carlisle Jun 13 '16 at 13:34
6

You have to use an expandable version of \ref, for instance with refcount.

\documentclass{article}
\usepackage{xstring,pifont,refcount}
\usepackage[colorlinks]{hyperref}

\protected\def\pding#1{\ding{#1}}
\newcommand{\subhelper}[2]{%
  \StrSubstitute{\tualek}{#1}{\pding{#2}}[\tualek]%
}

\newcommand{\dingbatise}[1]{%
  \edef\tualek{#1}%
  \subhelper{1}{202}%
  \subhelper{2}{203}%
  \subhelper{3}{204}%
  \subhelper{4}{205}%
  \subhelper{5}{206}%
  \subhelper{6}{207}%
  \subhelper{7}{208}%
  \subhelper{8}{209}%
  \subhelper{9}{210}%
  \subhelper{0}{74}%
  \tualek
}
\newcommand{\dref}[1]{%
  \hyperref[#1]{\dingbatise{\getrefnumber{#1}}}%
}

\begin{document}

\section{Etiam At Risus}\label{etiam}

In \dingbatise{120} pellentesque faucibus vestibulum \dref{etiam}.

\end{document}

enter image description here

| improve this answer | |
  • Thank you. Neat code! Would you mind explaining the logic behind \protected\def? – Taiki Jun 13 '16 at 13:48
  • 1
    @Taiki This avoids hassles with \noexpandarg and such. – egreg Jun 13 '16 at 13:49
  • OK, thanks. And if you don't mind another question, is it really necessary to explicitly declare refcount? I remove it and the code still works. – Taiki Jun 13 '16 at 13:56
  • 1
    @Taiki Apparently the last version of hyperref automatically loads refcount. It didn't use to, IIRC. – egreg Jun 13 '16 at 13:59
6

You could use zref to get an expandable \ref. It will also work with references which are not simple numbers:

\documentclass{article}
% For clickable refs:
\usepackage{hyperref,zref-user}
% For converting numbers to dingbats:
\makeatletter
\newcommand\dingref[1]{\zref@extractdefault{#1}{default}{0}}
\makeatother
\usepackage{xstring,pifont}
\newcommand{\dingbatise}[1]{%
\edef\tualek{#1}%
\noexpandarg%
\expandafter\StrSubstitute\expandafter{\tualek}{1}{\ding{202}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{2}{\ding{203}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{3}{\ding{204}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{4}{\ding{205}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{5}{\ding{206}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{6}{\ding{207}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{7}{\ding{208}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{8}{\ding{209}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{9}{\ding{210}}[\tualek]%
\expandafter\StrSubstitute\expandafter{\tualek}{0}{\ding{74}}[\tualek]%
\tualek
}
\begin{document}
\section{Etiam At Risus}\label{etiam}\zlabel{etiam}
\subsection{Blalb}\zlabel{subsec}
In \dingbatise{120} pellentesque faucibus vestibulum \dingbatise{\dingref{etiam}}.
\dingbatise{\dingref{subsec}}

\end{document}

enter image description here

| improve this answer | |

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.