2

Basically, I have an url in the form of "http://website.com/folder/this_is_a_pdf_document.pdf" which I want to display in my document in TeXStudio, however, an error pops up. It seemingly interprets the link as something to do with the math environment; i.e., what I get is this:

"Math command outside of math environment" whenever I hover, with my mouse, over the "_" symbols in the url, which TeXStudio have colored red.

In the "Errors" panel it says that a "$" is missing; which I take that it wants me to go into math mode, again. When the document is compiled and viewed by me, the link that I am trying to display has a crippling "this_is_a_pdf_document.pdf" part; namely, some of the letters are displayed as subscripts, which has, in all probability, have to do with those "_" that TeXStudio is not able to interpret in a non-math environment mode.

So, the question: How do I display the link without TeXStudio thinking that it has anything to do with math environment?

NB! It is not necessary for me to have the url linked a la hyperref usepackage; that is, I do not care that the url is not interactive and takes you to the actual website. Some of the urls that I've successfully displayed have no problems whatsoever (they do not have the "_" symbol in them). The working urls are displayed simply by:

\begin{center}http://www.website.com/setion\end{center}    

In respect to the hyperref usepackage, I've tried the following versions, none of which worked (TeXStudio interprets the "this_is_a_pdf_document.pdf" part of the url as containing math symbols):

\href{URL}{text}, \url{URL}, \nolinkurl{URL}, \hyperref{label}{text}, 
\hypertarget{name}{text}, and \hyperlink{name}{text} 

Thanks a lot!

  • 4
    Use the url package and the \url{...} command. If that is not enough, then please provide a minimal example and we can copy and work with. This makes helping a LOT easier. – daleif May 30 '14 at 14:54
  • 1
    Welcome to TeX.SE. While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. – Peter Grill May 30 '14 at 15:09
  • daleif's solution worked, thanks! Quite dumbfounded that it didn't work before... A follow-up question: now that the troublesome part of the url displays fine, is there a way to change the font style of the url? It seems to have the "typewriter" style, not the default style I've been writing in (roman, I believe). – Kas May 30 '14 at 15:28
3

\url, \href, and \hyperref support URLs, if package hyperref is loaded. Package url alone provides \url:

\documentclass{article}
\usepackage[colorlinks]{hyperref}

\begin{document}
\begin{itemize}
\item \verb|\url|:\\
  \url{http://example.com/folder/this_is_a_pdf_document.pdf}
\item \verb|\href+\nolinkurl|:\\
  \href{http://example.com/folder/this_is_a_pdf_document.pdf}{%
  \nolinkurl{http://example.com/folder/this_is_a_pdf_document.pdf}}
\item \verb|\hyperref+\nolinkurl|:\\
  \hyperref{http://example.com/folder/this_is_a_pdf_document.pdf}{}{}{%
  \nolinkurl{http://example.com/folder/this_is_a_pdf_document.pdf}}
\end{itemize}
\verb|\urlstyle{rm}|\urlstyle{rm}
\begin{itemize}
\item \verb|\url|:\\
  \url{http://example.com/folder/this_is_a_pdf_document.pdf}
\item \verb|\href+\nolinkurl|:\\
  \href{http://example.com/folder/this_is_a_pdf_document.pdf}{%
  \nolinkurl{http://example.com/folder/this_is_a_pdf_document.pdf}}
\item \verb|\hyperref+\nolinkurl|:\\
  \hyperref{http://example.com/folder/this_is_a_pdf_document.pdf}{}{}{%
  \nolinkurl{http://example.com/folder/this_is_a_pdf_document.pdf}}
\end{itemize}
\verb|\urlstyle{sf}|\urlstyle{sf}
\begin{itemize}
\item \verb|\url|:\\
  \url{http://example.com/folder/this_is_a_pdf_document.pdf}
\item \verb|\href+\nolinkurl|:\\
  \href{http://example.com/folder/this_is_a_pdf_document.pdf}{%
  \nolinkurl{http://example.com/folder/this_is_a_pdf_document.pdf}}
\item \verb|\hyperref+\nolinkurl|:\\
  \hyperref{http://example.com/folder/this_is_a_pdf_document.pdf}{}{}{%
  \nolinkurl{http://example.com/folder/this_is_a_pdf_document.pdf}}
\end{itemize}
Free text variants:
\begin{itemize}
\item \verb|\href|:\\
  \href{http://example.com/folder/this_is_a_pdf_document.pdf}{%
  free text}
\item \verb|\hyperref|:\\
  \hyperref{http://example.com/folder/this_is_a_pdf_document.pdf}{}{}{%
  free text}
\end{itemize}
\end{document}

Result

  • Extensive and informative! Just managed to change the font style of the url back to roman. Thanks everybody! – Kas May 30 '14 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.