39

I would like to write something like this inline:

This is paragraph element <p>

I'm using verbatim for writing the HTML element part, but cannot form everything in one line. I get something like this:

This is paragraph element
<p>

After looking through some answers on this forum I found that I should use \verb.
Can anyone help me correct my code:

This is paragraph element
\begin{verbatim}
<p>
\end{verbatim}
denisr
  • 491
  • 1
  • 4
  • 3
  • 3
    Welcome to TeX.SE. What is wrong with \verb!<p>!? There are also some packages that provide inline display for various programming languages , see listings or minted, for example –  Aug 22 '17 at 16:56
  • 3
    Did you try "This is paragraph element \verb+<p>+."? Note the use of the delimiter + to start and terminate the inline verbatim material. The delimiter can be any symbol that doesn't occur in the verbatim material. – Mico Aug 22 '17 at 17:00
  • @ChristianHupfer this works perfectly for me. I thought that using verbatim was the only way I could achieve this. Thanks. – denisr Aug 22 '17 at 17:01
  • this should provide additional useful information: When should one use \verb and when \texttt – barbara beeton Aug 22 '17 at 19:03

2 Answers2

32

To typeset inline verbatim-like material, it's best to use the macros \verb and \Verb; the latter is provided by the fancyvrb package. If that package is loaded and the instruction \VerbatimFootnotes is executed, one can even have \Verb instructions in footnotes. (One can't do this with \verb.)

Note that \verb and \Verb don't use matching pairs of curly braces ({ and }) to delimit their arguments; instead, use any non-letter symbol (except "*") that doesn't occur in the verbatim material itself. (Even \verb{<p>{ is legal, though I'll be the first one to state that it looks positively weird.)

If you prefer to render the inline verbatim material using the "regular" text font instead of a monospaced font, consider loading the listings package and writing "Paragraph element \lstinline{<p>}".

enter image description here

\documentclass{article}
\usepackage{fancyvrb} % for "\Verb" macro
\VerbatimFootnotes    % enable use of \Verb in footnotes

\setlength\textheight{3cm} % just for this example

\begin{document} \obeylines Paragraph element \verb+<p>+ Paragraph element \verb_</p>_ Paragraph element \Verb^<p>^ Paragraph element \Verb#</p># \VerbatimFootnotes Some text.\footnote{In a footnote: \Verb"<p>", \Verb:</p>:.} \end{document}

Mico
  • 506,678
  • "use any non-letter symbol": this was true before 2020/04/22, but now it should be replaced by "use any non-space symbol (it should be letter after space)". The change in LaTeX2e kernel was done by https://github.com/latex3/latex2e/commit/4f02ccd61d70534f80a96bf3fa1d304a996ac0ce#diff-311522c46cf73477c4cc418ef0908db3e36af092d34b7a2fbdc36d38e94805cc see lines 1162--1171. – wipet Mar 17 '21 at 06:06
  • @wipet - Many thanks for noticing and pointing out this significant change. I'll update my answer accordingly. – Mico Mar 20 '21 at 08:29
  • @wipet - Sorry for this long delay in getting back to you. I just checked, and \verb*<p>* still isn't valid syntax. I would appear that your claim, that "any non-space symbol" should be a permissible delimiter, isn't quite right. Or did I maybe miss something? – Mico Apr 14 '21 at 17:21
4

Code golfing with xparse and its v - argument specifier, meaning verbatim or just use \verb!<p>! for example.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xparse}

\NewDocumentCommand{\ShowInline}{v}{%
#1%
}

\begin{document}
Some HTML \ShowInline{<p>}

End tag for HTML\ShowInline{</p>}
\end{document}

enter image description here

  • 1
    Is there some advantage to using this approach to, say, loading the listings package and typing Some HTML \lstinline{<p>}? – Mico Aug 22 '17 at 17:23
  • 1
    @Mico: None, actually. That's why I called it code golfing ;-) –  Aug 22 '17 at 20:31