3

A paragraph I am writing contains a lot symbols such as "{" or "_". To display those symbols as text instead of command, I have to write them as "\{" and "\_". Otherwise, Latex will say there are errors. However, there are so many of those symbols in my document and it would be very inconvenient to have to add a dash in front of them every single time.

An example:

\begin{document}

The Tadrart Acacus (https://en.wikipedia.org/wiki/Acacus_Mountains) have a large variation of landscapes, from different-coloured dunes to arches, gorges, isolated rocks and deep wadis (ravines).

\end{document}

Refer to the wikipedia link: usually, I just copy the link from my browser and paste it into my paragraph. But due to the "_" symbol in the word "Acacus_Mountains", Latex will say there is an error and ask me to enclose it with the "$" symbol.

About the \verbatim environment: it would not work for me because:

  1. It changes the font of the paragraph.
  2. It does not go to a new line when reaching the margin.

To illustrate, I will enclose my paragraph with \verbatim:

  \begin{document}

  \begin{verbatim}

  The Tadrart Acacus (https://en.wikipedia.org/wiki/Acacus_Mountains) have a 
  large variation of landscapes, from different-coloured dunes to arches, 
  gorges, isolated rocks and deep wadis (ravines).

  \end{verbatim}

  \end{document}

this is what my text looks like inside the \verbatim environment: enter image description here

My question is: How to tell Latex that everything in a certain paragraph is to be interpreted as plain text?

  • 2
    It's very hard to answer the question incrementally (one complication at a time) if you refuse to give a complete example. :-) For instance, if you want \textbf{} to work, then that means you want not only the backslash to be treated as the escape character, but the { and } in \textbf{...}` to be treated as the begin-group and end-group characters, so that the argument to \textbf can work. But at the same time you say you don't want { to be treated specially. All of these constraints are difficult to satisfy simultaneously, especially when you don't say what they are. :-) – ShreevatsaR Mar 20 '18 at 17:54
  • 4
    For URLs there is the url package with its \url command. If you use hyperref (which loads url for you) the URL is even linked. – moewe Mar 20 '18 at 18:03
  • 1
    OK. Where does the _ appear in your document when it is not in a URL? (If you prefer your \urls in the same font as the rest of your docs, go with \urlstyle{same}.) – moewe Mar 20 '18 at 18:06
  • 3
    Fair enough. I guess I'm asking why it would appear. The underscore is not a natural feature of the English language or indeed any language I'm even remotely familiar with (Wikipedia tells me there are languages that use an underscore or a similar diacritical mark). So it is somewhat unusual if it were to appear out of the blue in your paragraphs. There are legitimate uses for _ in URLs, where I would use the \url command. There are also legitimate uses if you want to write computer code, where verbatim and friends are useful. I just can't think of another case where it would occur. – moewe Mar 20 '18 at 18:15
  • 1
    Once again, you actually don’t want “everything in a certain paragraph is to be interpreted as plain text”: it seems to me that you want the URL to be typeset correctly. For that, as you have already been told, the answer is to use the url package, or the hyperref package. – GuM Mar 20 '18 at 18:26
7

Here's my understanding of what you want, based on the question and comments:

  • You'd like certain characters (the underscore _ is one, and the brace { may be another but it's not clear as it contradicts the next point) to be treated in a not-special way (i.e. simply typeset that character).
  • Unlike verbatim, you'd like certain characters to continue to be treated in a special way, as TeX does by default: for example in \textbf{hello} you'd like the \ the { and the } to have their default (special) meanings.
  • Unlike verbatim, you'd like the font to remain normal, and the typesetting too (regular paragraphs with line-breaks, etc).

If this is correct, it may help you to understand what goes on normally. As TeX reads your input, it has a certain “catcode” assigned to each character. (See the answers to What are category codes? for more details.) Things with catcode 11 (“letter”) or catcode 12 (“other”) are simply typeset normally (specifically, they are treated as an instruction to typeset the glyph at that position in the currently loaded font), while others are treated specially.

By default, these are the characters that are treated specially:

  • \, {, }
  • $, &, #, ^, _, ~, %
  • space, end-of-line, control characters

I assume that you want to continue to treat the characters in the first (and third) group specially, but treat the characters in the second group as just text (in TeX terminology, as “other” characters). This you can do by defining an environment like the following simplechar:

output

\documentclass{article}
\usepackage[T1]{fontenc}

\newenvironment{simplechar}{%
   \catcode`\$=12
   \catcode`\&=12
   \catcode`\#=12
   \catcode`\^=12
   \catcode`\_=12
   \catcode`\~=12
   \catcode`\%=12
}{}

\begin{document}

\begin{simplechar}
This is a paragraph with \textbf{bold} and \emph{emphasized} text, but ``special'' characters like $ & # ^ _ ~ % are treated normally, while {braces} are not, and the paragraph uses a normal font and wraps (has line breaks) at text width.
\end{simplechar}

\end{document}

output

Compare this to the output you get with something like alltt, which is just a bit better than verbatim:

output with alltt

The default behaviour of TeX/LaTeX, of having all those characters treated as special, makes certain things very convenient to type (entering math mode, subscripts and superscripts, defining macros, etc), but can be a “gotcha” for beginner users who just want to typeset text and don't want any of that yet. See similar thoughts by ConTeXt user Aditya on his blog: Some thoughts on lowering the learning curve for using TeX.

  • @ASlowLearner: If what you want is to interpret all characters except `, {, and }` as “ordinary” characters, than load the alltt package and use the alltt environment it provides. – GuM Mar 20 '18 at 18:51
  • @GuM alltt doesn't do the same thing, for several reasons: (1) it switches the font to a monospaced one, (2) it doesn't typeset normal paragraphs, with line-breaking and respecting the current value of parskip and all that. (3) It doesn't cover the character % but this is a minor point and it's not clear whether the OP wants this anyway. In fact, if you look at the (short) implementation of the alltt package, almost everything it does is undesirable for the OP's use-case. – ShreevatsaR Mar 20 '18 at 18:57
  • You are right, mine was a stupid comment; I will not delete it, however, because I think that my foolishness may actually help other users not to make the same error as I did. :-) – GuM Mar 20 '18 at 19:01
  • Thank you. Your answer is exactly what I thought I would want, but now I just realized that it is not a very good idea since we rarely have a paragraph with only plain text, and changing the behavior of special characters is not worth the hassle. However, I just learned a whole lot more about Latex from your answer. – A Slow Learner Mar 20 '18 at 20:59
3

If the url package is loaded (preferably with the options hyphens, spaces, and obeyspaces) and the directive \urlstyle{same} is run, it's possible to encase entire paragraphs in \url directives. I wouldn't recommend it, though, as you'll lose hyphenation capabilities. For sure, if this were my document, I would apply \url directives only where necessary.

enter image description here

\documentclass{article}
\usepackage[hyphens,spaces,obeyspaces]{url}
\urlstyle{same}
\begin{document}

\noindent
The Tadrart Acacus (\url{https://en.wikipedia.org/wiki/Acacus_Mountains}) have a large 
variation of landscapes, from different-coloured dunes to arches, gorges, isolated rocks 
and deep wadis (ravines), along with several \url{my_matlab_code.m} files.

\smallskip\noindent
\url{The Tadrart Acacus (https://en.wikipedia.org/wiki/Acacus_Mountains) have a large 
variation of landscapes, from different-coloured dunes to arches, gorges, isolated rocks 
and deep wadis (ravines), as well as a few my_matlab_code.m files.}

\end{document}
3

enter image description here

\documentclass{article}

\usepackage[T1]{fontenc}

\def\zzz{%
   \catcode`\$=12
   \catcode`\&=12
   \catcode`\#=12
   \catcode`\^=12
   \catcode`\_=12
   \catcode`\~=12
   \catcode`\%=12
   \catcode`\\=12 
   \catcode`\{=12
   \catcode`\}=12
   \zzzz}
{
\catcode`\/=0
\catcode`\[=1
\catcode`\]=2
\catcode`\{=12
\catcode`\}=12
\catcode`\\=12
/long/gdef/zzzz#1\end{zzz}[%
#1/end[zzz]]
]


\begin{document}

\begin{zzz}
The Tadrart Acacus (https://en.wikipedia.org/wiki/Acacus_Mountains) have a large variation of landscapes, from different-coloured dunes to arches, gorges, isolated rocks and deep wadis (ravines).

and \ and & and # and } and {  not forgetting _ and ^ and %


\end{zzz}
\end{document}
  • +1, really cool trick for the braces! But the OP seems to also want \textbf{stuff} to work (result in bold stuff), so it's not clear (even to the OP) exactly what is desired with the braces. – ShreevatsaR Mar 20 '18 at 20:58
  • 1
    @ShreevatsaR does (s)he? the question says { should act like \{ (and I couldn't follow all the comments:-) – David Carlisle Mar 20 '18 at 21:01
  • Yes it's hard to follow :-) The second comment said “text format (such as bold, italics, etc.)”, and the fifth comment said “\textbf{} for bold”… basically I think the OP want(ed) something impossible: for both \textbf{...} to work, and for the braces to act like {. (Well it's probably not literally impossible, e.g. a LuaTeX filter or something that looks for space followed by } may work, but at least it's not clear.) – ShreevatsaR Mar 20 '18 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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