1

In my document for making url to math search website I need to produce urls like this:

https://website.xyz/search/?q=%24\frac{\pi r^2}{2}%24

I'm using pylatex and I see that it generates tex code as I expect:

\href{https://approach0.xyz/search/?q=%24\frac{m^2}{s}%24}{$\frac{d m_s}{ct^{2}}$}

But in resulting pdf I get modified url:

https://approach0.xyz/search/?q=%24\protect {\begingroup m^2\endgroup \over s}%24

And here's python code to generate it:

formula_with_href = pl.NoEscape('\\href{https://approach0.xyz/search/?q=%24' + '\\frac{m^2}{s}' + '%24' + '}{' + '$' + formula_latex + '$' + '}')

The script is too large to display, so here's mimimized LaTeX document:

\documentclass{article} 
\usepackage{hyperref} 
\begin{document} 
\href{https://approach0.xyz/search/?q=%24\frac{m^2}{s}%24}{$\frac{d m_s}{ct^{2}}$} 
\end{document}

So the question is how to make proper url.

5
  • 1
    Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Or, in your case, a runnable python script, that demonstrates the issue at hand...
    – DG'
    Oct 23, 2020 at 9:48
  • 1
    Your code is still not minimal, anyway...
    – DG'
    Oct 23, 2020 at 10:28
  • The problem is that I don't know for now what can be deleted from latex code
    – slavust
    Oct 24, 2020 at 11:10
  • Almost everything ;) -- This code would be sufficient to demonstrate your problem: \documentclass{article} \usepackage{hyperref} \begin{document} \href{https://approach0.xyz/search/?q=%24\frac{m^2}{s}%24}{$\frac{d m_s}{ct^{2}}$} \end{document}
    – DG'
    Oct 24, 2020 at 11:44
  • Thanks. Updated.
    – slavust
    Nov 8, 2020 at 11:17

1 Answer 1

0

For \href to work, you have to encode the url in percent-encoding, which python can do for you with urllib.parse:

import urllib.parse

domain = "https://approach0.xyz/search/?q="
formula = r"$\frac{d m_s}{ct^{2}}$"
query = urllib.parse.quote(formula)
formula_with_href = r"\href{" + domain + query + "}{" + formula + "}"

Which results in the correctly escaped string: \href{https://approach0.xyz/search/?q=%24%5Cfrac%7Bd%20m_s%7D%7Bct%5E%7B2%7D%7D%24}{$\frac{d m_s}{ct^{2}}$}

enter image description here

If you click on the formula, the website opens correctly:

enter image description here

1
  • 1
    Thanks! That worked.
    – slavust
    Oct 23, 2020 at 12:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .