Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to output the below text but since \ is a special character, I cannot:

[RegularExpression(@"\d+")]

Also sometimes I need use dollar sign $ as well but it seems to be a special char, too.

What is the way to escape those in LaTeX?

UPDATE:

I used \verb as suggested but I am unable to run commands inside it. Also, in the output the font and the opacity of the text is different than the default one:

\verb|[RegularExpression(\newline @"\d+")]|
share|improve this question
    

3 Answers 3

up vote 53 down vote accepted

(This is fairly basic stuff, but perhaps it's worth repeating it.)

The following ten characters have special meanings in (La)TeX:

& % $ # _ { } ~ ^ \

Outside \verb, the first seven of them can be typeset by prepending a backslash; for the other three, use the macros \textasciitilde, \textasciicircum, and \textbackslash.

\documentclass{article}

\begin{document}

\& \% \$ \# \_ \{ \}

\textasciitilde

\textasciicircum

\textbackslash

\end{document}

enter image description here

Note that the seven "single non-letter" macros don't gobble the space following them.

share|improve this answer
    
In pdfLaTeX I have defined a new command \textampersand by using \newcommand*{\textampersand}[0]{\&} or \renewcommand*{\textampersand}[0]{\&}. In XeLaTeX it is already available, I think. –  matth May 9 '12 at 17:35

Usually text like that is typeset in typewriter type and so there's a slick way to arrange it

\verb|[RegularExpression(@"\d+")]|

After \verb should go a character that's not used in the text to print "verbatim" and the same character should follow the text.

This command has a drawback: it can't be used in the argument of other commands.

There's a second "solution" which can come handy if it's needed a limited number of times:

\texttt{[RegularExpression(@"\string\d+")]}

where commands inside the argument to \texttt are allowed. It's not even necessary to use \texttt:

\textsf{[RegularExpression(@"\string\d+")]}

will work as well (when T1 font encoding is active) and will print the string in sans serif type.

share|improve this answer
1  
first solution is nice but I cannot use commands inside it. second solution breaks my entire document. Does every single little thing have to be so hard in LaTeX :) –  tugberk Nov 12 '11 at 16:22
    
Would you please modify your question and show some more cases? –  egreg Nov 12 '11 at 16:27
    
sure, see it. I updated! –  tugberk Nov 12 '11 at 16:31
2  
@egreg: in your definition of \pseudoverb, you've got [1} instead of [1]. And a drawback you don't mention of \detokenize is that it inserts spaces after macros names with more than one character (not sure if it's relevant for regular expressions). –  Philippe Goutet Nov 12 '11 at 22:11
    
@PhilippeGoutet Yes, you're right; I've deleted that part –  egreg Nov 12 '11 at 22:14

I needed a way to escape all special characters and I found this Perl function.

For example it will convert this:

& % $ # _ { } ~ ^ \ \today

into this:

\& \% \$ \# \_ \{  \} \texttt{\~{}} \^{} $\backslash$ $\backslash$today
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.