1

Surely I am missing something here, but I can't remember =(. I want to build a command that accepts special LaTeX symbols and correctly display them. As special I mean \, _, or some others, and respect spaces, page/line breaks. The special text can be in \rmfamily or \ttfamily and no hyphenation is desirable.

\documentclass{article}

\newcommand{\specialtext}{\ttfamily #1}

\begin{document}

\specialtext{This a special text with \ # _}

\end{document}
  • 2
    Do you mean \verb? – egreg May 2 '13 at 16:52
  • 1
    More or less like \verb, but with more options. After posting the question I thought about something like inline listings, but it seems to much for this operation. – cacamailg May 2 '13 at 17:01
  • To respect things like \rmfamily, I presume you mean that it is specified prior to entry into the command/environment, since otherwise it would display literally, rather than being enacted? – Steven B. Segletes May 2 '13 at 17:17
  • 1
    Yes you are right. – cacamailg May 2 '13 at 17:19
  • 1
    listings provides an inline command, \lstinline. It has an optional argument for all the settings you want. There's also \lstMakeShortInline which defines a shortcut for \lstinline where you can have preset options for the specified shortcut. – clemens May 2 '13 at 17:55
4

Thanks to a trick from David Carlisle (see Optional arguments in verbatim environments), he helped me with the following (EDITED for T1 encoding, per egreg's suggestion):

\documentclass{article}
\usepackage{verbatimbox}
\usepackage{readarray}
\renewcommand\encodingdefault{T1}
\parskip 1ex\parindent 0em

\newenvironment{venv}{\verbatim\venvinner}{\endverbatim}
\makeatletter
\newcommand\venvinner[1][]{{\nfss@catcodes\scantokens{\gdef\tmp{#1}}}\tmp}
\makeatother

\begin{document}

\normalsize
\begin{venv}[\LARGE]
this is a test of \LARGE verbatim
\end{venv}

Back to normal text outside of verbatim

\normalsize
\begin{venv}[\large]
\tiny this is a test of \large verbatim
that begins with a \ character, which
is tricky business when using optional 
arguments in a verbatim environment.
\end{venv}

Back to normal text outside of verbatim

\normalsize

\begin{venv}[\rmfamily]
Here is verbatim text in \rmfamily.
\end{venv}

\begin{venv}[\slshape]
And we have verbatim in \slshape
\end{venv}

\end{document}

enter image description here

p.s. I would add in closing that the verbatimbox package incorporates this kind of [optional pre-conditioning] logic in order to stuff verbatim into a box. But in that case, it must display on a single page, as a box cannot be broken across pages.

  • A backslash is printed as such if you use T1-encoded fonts. – egreg May 2 '13 at 21: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.