Debian Codegraph @

texmf-dist/doc/latex/enverb/doc-examples.tex

raw
1 \subsection{Examples}
2
3 The following preamble should work for all of the examples in this section
4 (except for the last one, which can be placed as is in the preamble since it
5 loads all the packages it needs).
6 \begin{ex}[no-tcb]
7 \documentclass{article}
8
9 \usepackage{enverb}
10 \usepackage{color}
11 \usepackage{multicol}
12 \usepackage{listings}
13 \end{ex}
14
15 \subsubsection{A Boxed Verbatim Display}
16 Defining a boxed verbatim macro is pretty simple, we just need to set up the
17 |bol| and |eol| hooks such that line breaks are respected, the rest of the setup
18 already works with \LaTeX's default definition for active spaces (and tabs).
19 \begin{ex}[store,no-tcb]
20 \newenvironment{myenv}
21 {\enverb{bol=\strut, eol=\strut\par}}
22 {%
23 \par
24 \medskip
25 \noindent
26 \fbox
27 {%
28 \parbox
29 {\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
30 {%
31 \raggedright\frenchspacing\ttfamily
32 \enverbBody
33 }%
34 }%
35 \par
36 \medskip
37 }
38 \end{ex}
39 The probably most complicated part there is the calculation of the |\parbox|'s
40 width, which is just a line width compensated by the space taken up by |\fbox|.
41
42 If we now use this environment we get a nice display of our verbatim contents
43 (you might want to play with this and see what happens if your input lines are
44 longer than a |\linewidth|; also note that the spaces after |\begin| and |\end|
45 are optional just like within the normal category code regime, \envlogo\ will
46 still find the end of its scope).
47 \begin{ex}[restore]
48 \begin {myenv}
49 \This is a%
50 \begin{environment}
51 that looks \good
52 \end {myenv}
53 \end{ex}
54
55 \subsubsection{A Page-Breakable Boxed Verbatim Display}
56 The previous example was boxed, lets put that to the next level. We can also
57 achieve page- (and column-)breakable boxed verbatim environments. The simple
58 minded trick here is to draw only line segments at the start and end of each
59 line using the |\vrule| primitive. As a result this will look horribly wrong for
60 lines longer than a single output line, but that would require too much hassle
61 (we just pay attention on our input formatting).
62
63 As an added bonus this should have line counters at the start of each line. For
64 that we first define a little helper.
65 \begin{ex}[store,no-tcb]
66 \newcount\myline
67 \newcommand\linecounting
68 {%
69 \advance\myline1
70 \textcolor[gray]{.7}{\footnotesize\the\myline}%
71 \quad
72 }
73 \end{ex}
74 Next we can define our environment. The |bol| and |eol| hooks look similar to
75 the previous example, but this time they got some added contents for the
76 additional formatting. Also, we borrow some code from \LaTeX's starred
77 \env{verbatim} to make spaces visible (since they are active in |\enverb| this
78 is also pretty straight forward).
79 \begin{ex}[store,no-tcb]
80 \newenvironment{myenv}
81 {%
82 \enverb
83 {
84 bol=\noindent\strut\vrule\hskip\fboxsep\linecounting
85 ,eol=\hfill\hskip\fboxsep\vrule\strut\par
86 }%
87 }
88 {%
89 \medskip
90 \hrule
91 \ttfamily\frenchspacing
92 \UseName{@setupverbvisiblespace}\UseName{@vobeyspaces}%
93 \enverbBody
94 \hrule
95 \medskip
96 }
97 \end{ex}
98 The entire result can then be used with ease.
99 \begin{ex}[restore,same-line=.38]
100 \begin{multicols}{2}
101 Text above
102 \begin{myenv}
103 This \is
104 {an} $example$
105 ^of verbatim_
106 input\, being
107 displayed boxed.
108 \end{myenv}
109 Text below
110 \end{multicols}
111 \end{ex}
112
113 \subsubsection{The Real Deal -- Using the Body Twice}
114 All of the above is just pretty playthings that are easily possible with
115 \envlogo\ (and with relative few lines of code) though most likely better done
116 by more full fledged packages like \pkg{listings} or similar. The original
117 reason for all this was to gain control over both the listing and typeset result
118 for code documentation. A simple example could be the following:
119 \begin{ex}[store,no-tcb]
120 \newenvironment{myenv}
121 {\enverb{}}
122 {%
123 \par\noindent
124 \begin{minipage}{0.48\linewidth}
125 \enverbListing{lstlisting}{}
126 \end{minipage}%
127 \hfill
128 \colorbox{yellow}
129 {%
130 \begin{minipage}{\dimexpr0.48\linewidth-2\fboxsep\relax}
131 \enverbExecute
132 \end{minipage}%
133 }%
134 \par
135 }
136 \end{ex}
137 Which if used looks like the following (admittedly not that pretty):
138 \begin{ex}[restore]
139 \begin{myenv}
140 This%
141 is%
142 great.%
143 \end{myenv}
144 \end{ex}
145
146 \subsubsection{\envlogo's Style}
147 And if you wondered, you guessed it, all examples in this documentation are
148 typeset using an \envlogo\ derived environment with a few bells and whistles
149 more (for instance collecting material, typesetting the verbatim listing, but
150 executing it together with a later code block).
151
152 The following is a complete listing of everything that sets up the looks of this
153 documentation's example blocks. Note that the macros starting with |\enverb@ex@|
154 are not internals of \envlogo\ but exclusive to the code formatting the
155 examples.
156 \begin{exwrap}{breakable}
157 \footnotesize
158 \lstinputlisting{preamble-examples.tex}
159 \end{exwrap}
160
161 And if we use that we can do all sorts of interesting things:
162 \begingroup
163 \NewEnvironmentCopy{ex2}{ex}
164 \begin{ex2}[same-line=.45]
165 We define a helper:
166 \begin{ex}[store,no-tcb]
167 \newcommand\foo{bar}
168 \end{ex}
169 And then can use it:
170 \begin{ex}[restore]
171 This is \foo
172 \end{ex}
173 A wider example:
174 \begin{ex}[below]
175 This is a wider example.
176 \end{ex}
177 An example with wide code
178 but small output:
179 \begin{ex}[same-line=.9]
180 \textcolor{red}{\bfseries.}
181 \end{ex}
182 \end{ex2}
183 \endgroup