# Some very odd behaviour of \verb and \verbatim in connection with TeXMaker

I cannot but assume that whatever <text> is placed between the delimiters (in this case +) in \verb+<text>+ and \verbatim+<text>+ has no effect whatsoever on the world outside the delimiters. The following MWE seems to tell us otherwise. \verb appears to effect the structure panel of TeXMaker suggesting this might be a TeXMaker-only problem (using Version 5.0.2), but \verbatim messes up the .pdf output as well as the structure panel, so I am completely puzzled.

\documentclass{book}
% RN. 11 Nov 2018
%=======================
\usepackage{verbatim}
\begin{document}
\section*{Preface}
"We wish to show that \verb+\part{name}\label{name}+ sets a label etc." promptly
introduces Part name in the structure section of the TeXMaker screen, however, no ill
effects in the .pdf output are observed.
\chapter{A}
\section{A1}\label{A1}
\subsection{A11}
The same effect is observed in the structure section of TeXMaker when using verbatim
in place of verb, i.e. \verbatim+\subsection{subname}\label{subname}+, except from this
point forward the .pdf output is screwed up as well.
\subsection{A12}
\chapter{B}
\section{B1}
\section{B2}
\end{document}

• Is \verbatim ever supposed to appear in a document? – egreg Nov 10 '18 at 15:29
• \verbatim is a syntax error, it is not surprising that syntax highlighters and editors do not pick up on all verbatim uses, yo see the same with syntax highlighting on this site, it is not possible without running a full tex execution to parse all instances of verbatim text correctly – David Carlisle Nov 10 '18 at 15:34
• the "warning" from tex (\end occurred inside a group at level 1) ### semi simple group (level 1) entered at line 14 (\begingroup) ### bottom level should be treated as an error message. – David Carlisle Nov 10 '18 at 15:36

The fact that \verbatim+x+ doesn't throw an error when the verbatim package is loaded is a by-product of how the verbatim environment is redefined.

If you don't load it, you get a much worse error, namely

Runaway argument?
+x+^^M\end{document}^^M
! File ended while scanning use of \@xverbatim.
<inserted text>
\par


followed by the input prompt, but you can only “pull the power chord” if you want to exit. With a front end using -interaction=nonstopmode you get instead

! Emergency stop.


It is always an error to use \xyz mid document when xyz is an environment. (The converse, that is using \begin{macro} and \end{macro}, is allowed when \macro has been defined with \newcommand).

For instance \flushleft mid document doesn't give an error, but messes up the output. The correct document level command is \raggedright.

The “macro forms” for an environment, that is \xyz and \endxyz after \newenvironment{xyz}{...}{...} can (mostly) be used in the definition of other environments, but they should always appear in the start and end part of the new environment's definition respectively. Beware that some environments do not allow this.

The verbatim package indeed recommends using \verbatim and \endverbatim for defining new verbatim-based environments. But \verbatim+x+ is definitely wrong.

About the structure laid out by TeXStudio: it cannot discriminate among \label or sectional commands appearing inside \verb or a verbatim environment. That's the way it is.

• In summary then, this was a case of making naive assumptions about the functionality of \verb+...+. And \verbatim needs to be used like an environment, i.e. \verbatim\endverbatim, using the syntax \verbatim+...+ causes havoc. – Reinhard Neuwirth Nov 10 '18 at 21:33
• @ReinhardNeuwirth \verbatim+...+ does not do anything sensible except messing up the output. Moreover, \verbatim and \endverbatim should only be used for defining new environments, not in the document body, as I wrote in my answer. – egreg Nov 10 '18 at 21:42