How to deal with hashtag (#) in \href

sc
scrive
Posts: 24
Joined: Mon Jul 06, 2020 8:05 am
Platform: Mac

Mon Jul 06, 2020 8:17 am Post

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!

User avatar
AmberV
Posts: 24424
Joined: Sun Jun 18, 2006 4:30 am
Platform: Mac + Linux
Location: Ourense, Galiza
Contact:

Mon Jul 06, 2020 7:40 pm Post

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:

Code: Select all

\href{https://www.literatureandlatte.com/contact-us#email}{https:\slash \slash www.literatureandlatte.com\slash contact-us\#email}
.:.
Ioa Petra'ka
“Whole sight, or all the rest is desolation.” —John Fowles

sc
scrive
Posts: 24
Joined: Mon Jul 06, 2020 8:05 am
Platform: Mac

Wed Jul 08, 2020 1:17 am Post

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:

Code: Select all

\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.
<to be read again>
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!

User avatar
AmberV
Posts: 24424
Joined: Sun Jun 18, 2006 4:30 am
Platform: Mac + Linux
Location: Ourense, Galiza
Contact:

Wed Jul 08, 2020 1:40 pm Post

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.
.:.
Ioa Petra'ka
“Whole sight, or all the rest is desolation.” —John Fowles

sc
scrive
Posts: 24
Joined: Mon Jul 06, 2020 8:05 am
Platform: Mac

Fri Oct 30, 2020 9:24 am Post

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:

Code: Select all

\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
:)

ju
juanoli
Posts: 1
Joined: Fri Oct 30, 2020 4:36 pm
Platform: Mac + Windows
Contact:

Fri Oct 30, 2020 4:41 pm Post

And how do you see that that works?

sc
scrive
Posts: 24
Joined: Mon Jul 06, 2020 8:05 am
Platform: Mac

Tue Nov 17, 2020 6:50 am Post

Hi juanoli,

Apologies for not responding sooner.

I was hoping to have found a solution to the hashtag (#) in \href issue before responding, but to no avail. My lack of familiarity with LaTeX is not helping.

My next thought is to see if one of the catcode commands might be of some help:

\catcode`\#=11 (Ref: https://arxiv.org/help/hypertex) or
\catcode`#=12 (Ref: https://tex.stackexchange.com/questions/447461/having-a-hash-sign-in-href-in-a-longtabu-cell?rq=1)

I'll post a response if I find something that helps.

Thanks,
scrive

User avatar
AmberV
Posts: 24424
Joined: Sun Jun 18, 2006 4:30 am
Platform: Mac + Linux
Location: Ourense, Galiza
Contact:

Tue Nov 17, 2020 3:36 pm Post

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.
.:.
Ioa Petra'ka
“Whole sight, or all the rest is desolation.” —John Fowles

me
merlonmi
Posts: 1
Joined: Thu Nov 26, 2020 2:04 am
Platform: Mac + Windows
Contact:

Thu Nov 26, 2020 2:07 am Post

The escape method never really worked for me.

User avatar
AmberV
Posts: 24424
Joined: Sun Jun 18, 2006 4:30 am
Platform: Mac + Linux
Location: Ourense, Galiza
Contact:

Thu Nov 26, 2020 2:02 pm Post

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.
.:.
Ioa Petra'ka
“Whole sight, or all the rest is desolation.” —John Fowles