6

I am trying to make a web URL display in my document:

\documentclass{article}  
\begin{document}  
\subsection{test}
\label{sec:test}

    [0] "test" $\emph{test}$. test, test. $\textrm{<http://tex.stackexchange.com/>}$. \\
\end{document}  

But everything shows up except that URL. Are there certain characters restricted even for textrm? I thought textrm displayed any character like it normally would with regular text.

Troy
  • 13,741
  • 2
    Use the package url which provides the command \url{}. Also, don't use dollar sign if the content is not mathematical. – Sigur Apr 29 '13 at 18:56
  • Welcome to TeX.sx! The `hyperref´ package is another alternative that provides clickable links in pdf. – Peter Jansson Apr 29 '13 at 19:10
  • Okay, I got /url{} working, but the font is different from the other text which all needs to be the same font. I am going to print this out for a report so it doesn't have to be clickable, but also I can't get the brackets <> around the URL to show up, even with using a backslash to try and escape the bracket characters. – guest56423 Apr 29 '13 at 19:14
  • @guest56423 I think it is a convention to have the urls in a "computerish" font. I think, your demand is best met as: \texttt{<http://tex.stackexchange.com/>}. – kan Apr 29 '13 at 19:17

2 Answers2

6
 $\textrm{<http://tex.stackexchange.com/>}$

You don't want math mode there, it is doing nothing other than slowing down the processing as \textrm gets you back to text mode. The < and > do not show up as the expected symbols as you are using the classic OT1 TeX encoding. You could use

\usepackage[T1]{fontenc}

to use an 8bit encoding in which the ASCII characters are in the usual slots but even if using that it is generally better to use \url from the url package or hyperref as that allows line breaking in a URL-aware manner, you can issue the command

\urlstyle{rm}

to make \url use the same font as \textrm.

David Carlisle
  • 757,742
5

You could load the LaTeX package called url and issue the command \urlstyle{same} in the document's preamble. That way, all material contained in various \url{....} directives will be typeset in current text font.

A big plus of using the \url directive for typesetting URL strings is that LaTeX will (generally) do a good job finding permissible line breaks in long URL strings.

Addendum: If you want to include "less than" and "greater than" signs at the start and end of a URL string, @GonzaloMedina has pointed out that one can do so nicely by loading the textcomp package and using that package's commands \textless and \textgreater. For instance, the code

\documentclass{article} 
\usepackage{textcomp,url} 
\urlstyle{same} 
\newcommand\myurl[1]{\textless\url{#1}\textgreater} 
\begin{document} 
\myurl{http://tex.stackexchange.com/} 
\end{document}

generates:

enter image description here

Mico
  • 506,678
  • With url’s own command and assuming David Carlisles advice with \usepackage[T1]{fontenc} is applied: \DeclareUrlCommand\url{\def\UrlLeft{<}\def\UrlRight{>}\urlstyle{same}}. But I would actually prefer \urlstyle{tt} or \urlstyle{sf} for better distinction. – Speravir Apr 30 '13 at 00:17
  • @Speravir - thanks for this. The OP had mentioned in one of the comments that he/she wants the URLs to be typeset in the same font as the surrounding text, so that's why I'm setting \urlstyle{same}... – Mico Apr 30 '13 at 01:22