How to deal with hashtag (#) in \href

I’ve tried using the escape method (#) whenever there is a hashtag in a \href{ statement, but to no avail.

I’ve also tried in Compile ‘Replacements’ to replace all \href{ statements with \url{, but that doesn’t work either (nothing gets replaced for some reason).

There are a number of websites that use the hashtag character and it is problematic whenever \href is used.

Is there any way to deal with a hashtag in a \href statement ?

Thanks!

It would help to know a little of your workflow, to understand the context of where this is happening. I’m speculating, based on the syntax of the link examples you’re providing, that you use LaTeX? If so, what approach are you taking for generating the .tex itself—is this all hand-typed, possibly with the help of our LaTeX template, or are you using a conversion layer like Markdown?

To speak of LaTeX in general, it is acceptable to leave the hash mark intact in the href itself, in my experience. It’s the printed text that you have to escape. You would want something like this, for a hyperlink that prints itself in the output:

\href{https://www.literatureandlatte.com/contact-us#email}{https:\slash \slash www.literatureandlatte.com\slash contact-us\#email}

Hi AmberV,

Thank you for your response. Apologies for not providing a few more details.

The offending \href statement containing the hashtag is in a footnote within a caption, inside a figure.

Here is the Scrivener compiled code from the “.tex” file:

\includegraphics[width=466pt,height=417pt]{41597_2020_530_Fig3_HTML.png}\caption{Global mean surface temperature from the Temperature 12k database using different reconstruction methods. The fine black line is instrumental data for 1900--2010 from the ERA-20C reanalysis product. The inset displays an enlarged view of the past 2000 years. See Fig. \href{https://www.nature.com/articles/s41597-020-0530-7#Fig2}{2} for additional explanation.}\label{fig:GlobalMeanTemp} \end{figure}

The offending hashtag is in the next-to-last line of the \includegraphics code shown above, e.g.:

\href{https://www.nature.com/articles/s41597-020-0530-7#Fig2}{2} for additional explanation.}

The typesetting console error that occurs is:

! Illegal parameter number in definition of \GetTitleStringResult.

F
l.1997 …-7#Fig2}{2} for additional explanation.}

If I remove the hashtag, the Latex typesetting completes without erring out, but of course the \href link is no longer valid.

Any ideas or thoughts would be greatly appreciated.
Thanks!

I’m not familiar with the particular error you are coming across, but in doing a few quick searches in the Web, it seems that there may be some fragility in putting a URL like this into a float, which a figure environment would be. You might have some luck looking for a solution on the TeX StackExchange, or at least post a new question to get help from the LaTeX community.

Hi AmberV,

Thank you (again) for your responses.

I wanted to followup on the threads from last July regarding the appearance of the hashtag (#) in \href.

I have found that the same compile error issue with \href{} appears when the hashtag (#) is used in a \url{} command such as:

\url{https://en.wikipedia.org/wiki/Global_catastrophic_risk#Anthropogenic}

Since last July, I’ve found two links to LaTeX coding sites that offer possible solutions:

  1. https://latex.org/forum/viewtopic.php?f=50&t=15632&sid=f3af07e588676f15f61dcd60406af094

  2. https://github.com/latex3/hyperref/issues/110

As best I can tell, the ‘solutions’ offered on the above web sites either do not work for me or do not directly address the errors that appear at compile time whenever the hashtag (#) appears in a \url or \href command. Escaping the # character (#) allows the compile to finish without error, but then the links are incorrect and an error occurs when clicking on the link to open the web page.

As best I can tell, this appears to be a LaTeX issue, not a Scrivener issue, but I thought I’d followup the earlier thread given the links I found on latex.org and gitHub.com.

For now, I am manually adjusting the \href and \url links to eliminate or escape the hashtag (#) symbol but this does not solve the underlying issue.

On the latex.org website, there is some conjecture that the problem lies with how “\itshape sets everything in italics” and that their solution using “\harvardurl” works " (as long as you don’t use the hyperref package)", but this is not an option for me.

On the gitHub.com website, they suggest using “a bitly link”, but I feel this is a bit of a kludge as it needs to be maintained manually.

What is a bit odd about the issue is how well the \url{} command is at handling all other special characters that can appear in a hyperlink, but seems to choke when it comes to the hashtag (#) character.

I’m curious if anyone else has run into this issue, and if so, if there are any solutions other than what is offered on either the latex.org or the gitHub.com websites. It would not be the first time I’ve missed something that may be obvious to others.

Thank you for all your help,
scrive
:slight_smile:

And how do you see that that works?

In your shoes, I would probably remove the image from the figure float and handle it manually. It would require checking the page whenever content shifts, to ensure its placement hasn’t become awkward, but you kind of need to do that every time anyway as part of the final “galley proof” phase. That may not work for you though, if you need a table of figures somewhere.

The escape method never really worked for me.

I’m not sure what you mean to be adding to this discussion. Escaping the hash is what you would do in 99.9% of cases. This is about an extremely narrow edge case where escape doesn’t work inside of a floating box, it seems the float is intercepting the hash at a deeper level than the escaping code, and confusing it with an argument call.