I have a problem with adding text into a LaTeX document. I have a LaTeX document about LaTeX and I need to insert some code snippets. I need to add "\usepackage{musixtex}" as a text, which should be displayed as a text. Do you know how?
[Tex/LaTex] Inserting LaTeX code into LaTeX
verbatim
Related Solutions
Here is an adaptation of Herbert's solution. The macro \DisplayAsLdots{}
is used to wrap the contents of the source code which you want replaced with ...
. It uses \immediate\write18
to pre-processes the \jobname.vrb
file through a sed
script (thanks to David Carlisle and egreg) to produce the source code displayed on the right:
Notes:
- This will only work on a Unix OS.
- As Stephen Lehmke pointed out, there can not be any line breaks within the
\DisplayAsLdots{}
macro. Since
sed
is greedy it will effectively gobble any other macros (and any content in between) that begin and end on the same line. So, code such as:\DisplayAsLdots{foo} xyz \label{bar}
will be treated as if were (note where the parameter to
\DisplayAsLdots
ends):\DisplayAsLdots{foo xyz \label{bar}}
Further Enhancements:
- Adapt to work on non-unix operating systems (perhaps a pure LaTeX solution that does not require temporary files).
- Use
\ldots
instead of three periods. - Enhance the regular expression for
sed
to allow for line breaks in\DisplayAsLdots
, and terminate the replacement with the proper matching closing brace (to fix 3rd point in Notes section above).
Comment on this Presentation Style:
As mentioned in the comments, I really dislike this method of presentation.
This stems from the fact that this documentation appears as if it is intended to illustrate how to use itemize
lists to novice users -- My feeling is that anything that could possibly distract the reader should be minimized. In this example there are two issues that come to mind:
- The use of the
. . .
(and the associated missing text). - The fact that there is an alignment between the input and the output.
With LaTeX there really is no such thing as a line by line comparison with the input and the output, so to show an example where things appear to be aligned will require additional work from the reader to comprehend.
I would rather see the full input text and output so I can see the correspondence between the input and the output.
Keep in mind that this is just personal opinion, others might prefer this style.
Code:
\documentclass{article}
\usepackage{enumitem}
\usepackage{blindtext}
\usepackage{fancyvrb}
\usepackage{listings}
\setlist[itemize]{leftmargin=*}%
\newcommand*{\DisplayAsLdots}[1]{#1}%
\newlength\CodeWidth
\makeatletter
\newenvironment{ShowCode}[2][]
{\lstset{#1}\VerbatimEnvironment%
\global\CodeWidth=#2%
\begin{VerbatimOut}{\jobname.vrb}}
{\end{VerbatimOut}\settowidth\@tempdima{\lstinputlisting{\jobname.vrb}}%
\ReplaceDisplayedLdots%
\noindent%
\minipage[t]{\CodeWidth}\vspace{0pt}\input{\jobname.vrb}\endminipage\hfill%
\minipage[t]{\dimexpr\@tempdima-2\fboxsep-2\fboxrule\relax}
\vspace{0pt}\lstinputlisting{\jobname.vrb.new}\endminipage}
\newcommand{\ReplaceDisplayedLdots}{%
\immediate\write18{%
cat \jobname.vrb%
| sed -e 's|\string\\DisplayAsLdots{.*}|...|'%
> ./\jobname.vrb.new%
}%
}%
\makeatother
\begin{document}
\blindtext
\begin{ShowCode}[language={[LaTeX]TeX},frame=single,columns=fixed,
basicstyle=\small\ttfamily,breaklines,
keywordstyle=\bfseries,
]{0.50\linewidth}
\begin{itemize}
\item Each list is \DisplayAsLdots{marked with a \emph{label}. The labels in this itemized list are} bullets.
\item List can be \DisplayAsLdots{nested within} one another.
\begin{enumerate}
\item The item labels \DisplayAsLdots{in an enumerated list are numberals or} letters.
\item A list should \DisplayAsLdots{have at least} two items.
\end{enumerate}
\LaTeX\ permits at least four levels \DisplayAsLdots{of nested lists, which is} more than enough.
\item Blank lines \DisplayAsLdots{before an item} have no effect.
\end{itemize}
\end{ShowCode}
\end{document}
If you want to render LateX syntax harmless in user input it either requires balanced braces (so that you can pick up the material as an argument and pass it through \scantokens
) or it requires es sequence of "stop" tokens that are known not to be part of the material, e.g., \end{verbatim}
or +
in in \verb+foo+
.
Neither can be guaranteed if you have no control whatsoever about the user input, e.g., what would happen if you user writes: "This text contains \end{verbatim} and now I'm free to use \LaTeX{} code."? Then it will blow up or at least execute the second part.
So if safety is important then XSLT is the way to go in my opinion. If you can live with that danger than consider changing \verbatim@font
which is command that sets up the font used in verbatim
and \verb
. Its default definition is
\def\verbatim@font{\normalfont\ttfamily}
So if you change this to do "nothing" then you would get the normal body font.
Update (sorry wrong time of the day)
Of course that doesn't solve the fact that "verbatim" does not wrap. To fix this what is really needed is to write your own version of a "verbatim" environment" that doesn't change spaces to active and doesn't add
\obeylines \verbatim@font \@noligs
\hyphenchar\font\m@ne
\everypar \expandafter{\the\everypar \unpenalty}%
in the first place, as those are your offenders.
Best Answer
You can use
\verb
like this:Edits by Speravir and Sam Whited:
There is also a starred version, which makes the spaces visible:
Multiline
If you end up with more than a short, one line, phrase you might also try the
verbatim
(built in) orVerbatim
(an improved version located in thefancyvrb
package) environments.eg.