GeneralPadding Zeros in an \href

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
chicagocoyote
Posts: 2
Joined: Tue Sep 30, 2025 9:18 pm

Padding Zeros in an \href

Post by chicagocoyote »

I am using \newcounter{} to create integers used in links to audio files in my PDF document. I need to zero-pad these integers to "2" spaces.

Note that in my code examples, I have substituted the string "xxxxx" for "https". I have done this because the site formatter takes things that look like an html link and abbreviates them, thus, making some of my latex code disappear.

In the header I have:

\newcounter{ChChCounter}
\newcounter{ChExCounter}

\newcommand{\AudioLinkStr}{%
xxxxx://fredgeorge.com/the\_book/ex\_\padzeroes[2]{\decimal{ChChCounter}}-\padzeroes[2]{\decimal{ChExCounter}}.mp3
}

and so, in my document, the command \AudioLinkStr will create strings that look like:

xxxxx://fredgeorge.com/the_book/ex_06-01.mp3

which is what I want.

BUT, when I include \AudioLinkStr in a \href{}{} construct as follows:

\href{AudioLinkStr}{\textbf{Audio Example Whatever}}

a clickable link is created in my document, BUT the link does _NOT_ go to the audio example on the web in a browser. Instead the link tries to open the document "AudioLinkStr.pdf" with a PDF viewer!

When I change the argument in \href from AudioLinkStr to \AudioLinkStr, building the document returns the error:

! TeX capacity exceeded, sorry [input stack size=10000].
\@protected@testopt ...estopt \else \@x@protect #1
\fi
l.128 \href{\AudioLinkStr} {\textbf{Audio Example Whatever...
! ==> Fatal error occurred, no output PDF file produced!



How can I fix the above? What am I doing wrong?

Thank you for your help.



P.S. I wonder if the problem is related to the fact that I have underscores in my web link. Notice that in the above I have to format the link with back slashes before the underscores:

xxxxx://fredgeorge.com/the\_book/ex\_ ...

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
Stefan Kottwitz
Site Admin
Posts: 10362
Joined: Mon Mar 10, 2008 9:44 pm

Re: Padding Zeros in an \href

Post by Stefan Kottwitz »

Hi,

welcome to the forum!

That may be solved with \expandafter, like this:

Code: Select all

\expandafter\href\expandafter{AudioLinkStr}{\textbf{Audio Example Whatever}}
If that doesn't work for you, perhaps copy a small (complete) example here that doesn't work. Use the "Code" button, then https is not parsed, no problem at all, and the code is even compilable online here. Like this:

Code: Select all

\newcommand{\AudioLinkStr}{%
https://fredgeorge.com/the\_book/ex\_\padzeroes[2]{\decimal{ChChCounter}}-\padzeroes[2]{\decimal{ChExCounter}}.mp3
}
Stefan
LaTeX.org admin
chicagocoyote
Posts: 2
Joined: Tue Sep 30, 2025 9:18 pm

Re: Padding Zeros in an \href

Post by chicagocoyote »

Thank you for your response. I was not able to get \href{} to work with \expandafter, however.

Included here is a complete latex example which illustrates my problem. \AudioLinkStr _is_ returning a correctly formatted string, but I cannot get \AudioLinkStr to work in \href

Thank you all for taking a look.

Code: Select all

\documentclass[11pt]{book}

\usepackage[colorlinks = true, linkcolor = blue, urlcolor = blue, citecolor = blue, anchorcolor = blue]{hyperref}
\usepackage{fmtcount}

\newcounter{ChChCounter}
\newcounter{ChExCounter}

\newcommand{\AudioLinkStr}{%
https://soundcloud.com/user-145142078/ex\_\padzeroes[2]{\decimal{ChChCounter}}-\padzeroes[2]{\decimal{ChExCounter}}
}

\begin{document}

\setcounter{ChChCounter}{6}
\setcounter{ChExCounter}{1}

% THIS INDICATES THAT MY STRING IS FORMATTED CORRECTLY
% https://soundcloud.com/user-145142078/ex_06-01
\AudioLinkStr

\vspace{0.1in}

% THIS SHOWS THAT THE HARD CODED STRING WILL WORK IN THE \href{} CONSTRUCT
\href{https://soundcloud.com/user-145142078/ex_06-01}{\textbf{Audio Example \theChChCounter-\theChExCounter}}

\vspace{0.1in}

% BUT THIS LINK DOES NOT WORK
\href{AudioLinkStr}{\textbf{Audio Example \theChChCounter-\theChExCounter}}

\vspace{0.1in}

% AND I DID NOT GET \expandafter TO WORK AS WELL
\expandafter\href\expandafter{AudioLinkStr}{\textbf{Audio Example \theChChCounter-\theChExCounter}}

\vspace{0.1in}

\end{document}


Post Reply