I’m trying to include a URL in my LaTeX document, but when I compile it, the formatting looks odd, parts of the URL are rendered like subscripts, and the font appears inconsistent.
I suspect this is due to special characters like underscores being misinterpreted by LaTeX.
What’s the correct way to insert a LaTeX URL so it renders cleanly and, ideally, as a clickable link?
Should I use the hyperref package or is there another approach that works better for both plain text output and PDF links?
Would appreciate a simple example or best practice for handling URLs in LaTeX documents.
I’ve run into this exact problem while writing academic papers.
The most reliable way to insert a LaTeX URL is by using the \url{}
command from the hyperref package.
It handles all the special characters (like underscores) and keeps the formatting consistent.
Add this to your preamble:
\usepackage{hyperref}
Then in your document:
\url{https://example.com/this_is_a_test}
This will render the URL with proper spacing, preserve underscores, and make it clickable in the PDF.
It’s the most widely recommended way to embed LaTeX URL links. Happy to help 
If you want the link to be clickable but don’t want to show the entire LaTeX URL (maybe it’s too long or has ugly parameters), \href{}
is a great alternative.
I often use this in slides or reports when aesthetics matter more.
Example:
\href{https://example.com/this_is_a_test}{Visit Example}
This will show “Visit Example” as a clickable text linking to the actual URL, and avoids any formatting weirdness entirely.
There was a time I had to avoid external packages due to submission restrictions.
If you’re not using hyperref, you’ll need to manually escape special characters in the LaTeX URL:
https://example.com/this\_is\_a\_test
Underscores (_) must be escaped as \_
, and in some cases, you may also want to enclose the whole thing in \texttt{}
for monospace font consistency:
l\texttt{https://example.com/this\_is\_a\_test}
It’s more tedious, but it works when you can’t rely on packages.