Explanation:
I am using twocolumns
so you can see how the lines will break with each option (and how bad it can get).
The first option (texttt
) will produce the desired result since you have no characters to escape in the present case, but the line breaking will be messed up because TeX is not geared for this.
The second option (\verb
or verbatim
environment) is what you should use for code, but by default it doesn't try to break lines, and you have to do it manually.
The third option is to load a package that allows you to have fancier verbatim text; for this I chose listings
and set it up so that it would use the typewriter font and break lines automatically. You can see that it does a much better job than the first two options. You should have a look at the documentation if you want to read about the other options (syntax colouring, etc.)
\documentclass[twocolumn]{article}
\usepackage{listings}
\lstset{
language=XML,
basicstyle=\ttfamily,
breaklines=true,}
\begin{document}
With \textbackslash texttt\{...\} (plain, simple typewriter face): \texttt{<context:component-scan base-package="com.secret.internal.secret" />}
\bigskip
With \textbackslash verb+...+ (inline verbatim): \verb+<context:component-scan base-package="com.secret.internal.secret" />+
\bigskip
With the ``verbatim'' environment (display verbatim, not indented):
\begin{verbatim}
<context:component-scan base-package="com.secret.internal.secret" />
\end{verbatim}
\bigskip
With the ``listings'' package, inline verbatim: \lstinline+<context:component-scan base-package="com.secret.internal.secret" />+
\bigskip
And display verbatim (still with``listings''):
\begin{lstlisting}
<context:component-scan base-package="com.secret.internal.secret" />
\end{lstlisting}
\end{document}
(Don't forget to add the preamble if you choose to use listings
)
$
will simply turn on math-mode, so that would not so it for you.