5

I am trying to write some XML tags as part of my text but I can not get the < and > symbols to come out right in the output. What am I missing?

I have tried both <tag> and \<tag\>, the former results in ¡tag¿ and the later in error messages.

The document class is report and the environment is document.

2

2 Answers 2

6

For formatting tags like <tag> prefer to wrap them into \verb|<tag>|.

Also, I'd recommend to create a custom command, like this (this will allow you to change the typesetting of all XML tags at once):

\documentclass[varwidth]{standalone}
\usepackage{xcolor}
\newcommand{\tag}[1]{\texttt{\textbf{\textcolor{red}{<}}#1\textbf{\textcolor{red}{>}}}}
\begin{document}
This is an XML \verb|<tag>| example.

This is an improved \tag{tag} example.
\end{document}

Result:

enter image description here

2

If you're going to have to type up more of these tags, the listings package may be well worth a try (employing colors for operators (or literals, or whatever) is tricky; if you'd like them, usually, one suggests the package minted, which unfortunately does not do inline stuff! catch 22!):

\documentclass{article} 
\usepackage{listings} 
\begin{document}
      \lstinline[language=xml]!<tag>!  
\end{document} 

Some remarks about the code:

  1. ! is a delimiter for the code that goes between its two instances: so, this should be a symbol that does not appear in the code (cf. | that appears in \verb command in m0nhawk's solution). So,

    \lstinline<<tag>< % wrong
    \lstinline><tag>< % not wrong, but not what you want  
    \lstinline$<tag>$ % right 
    \lstinline+<tag>+ % right
    \lstinline|<tag>| % right 
    
  2. The delimiter cannot be this [ unless you do the following:

    \lstinline[][<tag>[
    

    (What is happening is that, the \lstinline looks for an optional list of key=value pairs, when it first encounters [.)

  3. I am posting this answer, mostly for the reason that, using \verb, you end up with different font family, which may well be your desire! In case, you'd like the same font family, this solution is open.

Output:

lstinlinefun.png

2
  • In this case I'm indifferent about the font or color, but it's good to know the options. It's just for a handful of tags in thi text and the font resulting from using \verb looks fine in the output so I'll stick to using that. Commented May 4, 2013 at 13:55
  • Sure thing! :) As I said, I am posting this mostly for variety, but also we can refer to this later on.
    – kan
    Commented May 4, 2013 at 13:56

You must log in to answer this question.

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