GeneralPadding Zeros in an \href

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
chicagocoyote
Posts: 3
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: 10365
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: 3
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}


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

Re: Padding Zeros in an \href

Post by Stefan Kottwitz »

Both versions work basically:

Code: Select all

\documentclass{book}
\usepackage[colorlinks = true, urlcolor = blue]{hyperref}
\newcommand{\AudioLinkStr}{https://soundcloud.com/user-145142078/}
\begin{document}
\href{\AudioLinkStr}{\textbf{Audio Example}}

\expandafter\href\expandafter{\AudioLinkStr}{\textbf{Audio Example}}
\end{document}
However, that \padzeroes code causes an issue. I tried to expand it with \edef and \xdef, which causes other errors. Perhaps some other package works, or LaTeX's internal \two@digits command.

Stefan
LaTeX.org admin
chicagocoyote
Posts: 3
Joined: Tue Sep 30, 2025 9:18 pm

Re: Padding Zeros in an \href

Post by chicagocoyote »

Thank you for taking a look.

So \href does not work when using \padzeroes in the argument.

Could this be considered a bug?

Could this be put on a bug list somewhere?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10365
Joined: Mon Mar 10, 2008 9:44 pm

Re: Padding Zeros in an \href

Post by Stefan Kottwitz »

Since it occurs with hyperref and not in regular text, you could post it as a Github issue. I recommend not calling it a bug, but a limitation or a problem. Such issues arise when complex macros are expanded and rewritten to create hyperlinks. There are commands to work around such issues, such as \protect and \expandafter. The hyperref developers may be used to solve such issues.

Stefan
LaTeX.org admin
Post Reply