4

How to escape this kind of expression and make in typewritter font?

I added $ dollars to both ends, it did work - warnings dissapear. But text became italic of unknown font and it doesn't jump on next line upon reaching margins, but goes directly to the edge of the page.

$  
<context:component-scan base-package=``com.secret.internal.secret'' /> 
$
3
  • $ will simply turn on math-mode, so that would not so it for you.
    – ienissei
    Commented Apr 21, 2012 at 6:34
  • actually I found solution {\ttfamily <context:annotation-config /> } - it seems typewritter escapes characters automatically and I was using it incorrectly. Should I close the thread?
    – Aubergine
    Commented Apr 21, 2012 at 6:43
  • See my answer, there are better ways than using plain typewriter font, although that may be overkill if you only have this one bit of code to put into your document.
    – ienissei
    Commented Apr 21, 2012 at 6:51

1 Answer 1

8

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)

enter image description here

2
  • Good markup suggestions but a warning to use straight double quotes "com.secret.internal.secret" the left-right quotes were probably done by a "smart" editor but it is best to stop that for XML. The XML is not well formed with left/right quotes and it is a very common problem in XML forums that people cut ad paste examples from "typeset" examples and wonder why they get parse errors. Commented Apr 21, 2012 at 8:54
  • @DavidCarlisle Thank you, I have edited my answer. I just copied the OP's text and hadn't replaced the quotes.
    – ienissei
    Commented Apr 21, 2012 at 12:47

You must log in to answer this question.

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