22

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}
4
  • 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
    – user31729
    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 Aug 22 '17 at 19:03
18

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} 
3
  • "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 github.com/latex3/latex2e/commit/… see lines 1162--1171.
    – wipet
    Mar 17 at 6:06
  • @wipet - Many thanks for noticing and pointing out this significant change. I'll update my answer accordingly.
    – Mico
    Mar 20 at 8: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 at 17:21
3

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

2
  • 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 ;-)
    – user31729
    Aug 22 '17 at 20:31

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.