summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/numberedblock/numberedblock.sty
blob: 1c2cf01cd9a52feed9e57206dbb4c06181500572 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
\ProvidesPackage {numberedblock} [2014/02/24 v1.10]
%
% by Steven B. Segletes
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% numberedblock provides several routines 
% intended to print out a block of code with a unique,
% sequentially indexed label.  The code block is printed to the left,
% with the block label printed centered vertically on the code block,
% horizontally right-justified.  Each line of code is not numbered... 
% rather, the block of code gets a single label.
%
% There are three parameters with which the user may freely alter:
% 1) maxblocklabelsize is a length set aside for the code-block labels.
%    If it is too small, the label will run off the right margin; too large
%    and it limits the width of the code block itself.  However, by setting
%    this parameter to a negative value, it will force the label to be
%    offset into the right margin, which may be a desirable feature.
% 2) blockindent is a length defining the left-side indent to be used
%    for the code block to be printed.
% 3) blocklabel is the command that actually formats the block label to
%    the desired appearance.  Currently, italicized numbers are used within
%    non-italicized square brackets to comprise the block label.
%
% There is a command-line version called numblock, which cannot
% handle verbatim input.  And there is an environment called numVblock
% which handles cases invloving verbatim input
%
% An example of how to use numberedblock is given below.  The
% double-backslash is used for linebreaks in the code. Tildes need to
% be used for hard spaces under two conditions: when the code contains
% multiple sequential spaces; and if a space follows a period (which
% would otherwise invoke end-of-sentence spacing considerations).  In
% addition, special LaTeX characters need to be quoted in the
% appropriate manner of LaTeX.
%
%   \numblock{Line 1 of code\\Line 2 of code\\Line 3...\nblabel{mylabel}}
%
% A label may set to the code block with \nblabel{}.
%
% In contrast, the numVblock environment is a verbatim environment.
% A label may be passed to numVblock through an optional argument, via 
% \nbVlabel{}.
%
%   \begin{numVblock}[\nbVlabel{mylabel}]
%         program test
%         implicit none
%         integer a, x
%         a = 0
%         x = 1
%      10 a = a + x
%         if (a .eq. 100) stop
%         goto 10
%         end
%   \end{numVblock}
%
% The font used for the code block itself is the ttfamily (typewriter)
% of fixed-width characters.  The counter named blocknum is used to
% index the code block sequence.
%
% V1.1 - Added the hyperref-compatible capability to label and
%        reference numbered blocks.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NeedsTeXFormat{LaTeX2e}
\@ifundefined{verbatim@processline}{\RequirePackage{verbatim}}{}
\usepackage{verbatimbox}

%%%%% LABELING CAPABILITY
% See: http://tex.stackexchange.com/questions/160466/
%      how-can-one-refer-to-a-part-of-an-equation
\AtBeginDocument{\let\nb@label\label}
\newcounter{nb@counter}
\newcommand{\nblabel}[1]{\def\@currentlabel{\theblocknum}\nb@label{#1}}
\newcommand{\nbVlabel}[1]{\setcounter{nb@counter}{\theblocknum}\stepcounter{nb@counter}%
  \def\@currentlabel{\thenb@counter}\nb@label{#1}}
%%%%%

\newsavebox{\@savedverbbox}
\newlength\maxblocklabelsize
\newlength\blockindent
                                  \setlength\maxblocklabelsize{-0.4in}
                                         \setlength\blockindent{0.2in}
                     \newcommand\blocklabel[1]{[\textit{\arabic{#1}}]}
\newcounter{blocknum}
\setcounter{blocknum}{0}
\newlength\codeblockwidth
\newlength\parindentsave
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\numVblock{%
  \sbox\@savedverbbox{\usebox{\savedverbbox}}
  \verbbox
}
\def\endnumVblock{
  \endverbbox
  \numblock{\theverbbox[t]}
  \global\sbox{\savedverbbox}{\usebox{\@savedverbbox}}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\numblock[1]{
  \setlength\codeblockwidth{\textwidth}
  \addtolength\codeblockwidth{-\maxblocklabelsize}
  \addtolength\codeblockwidth{-\blockindent}
  \setlength\parindentsave{\parindent}
  \parindent 0in
  \addtocounter{blocknum}{1}
  \vspace{\abovecaptionskip}
  \begin{tabular}{@{\hspace{\blockindent}} l @{} r @{}}
     {\tt
      \begin{tabular*}{\codeblockwidth}{@{} l @{}}
        #1
      \end{tabular*}%
     }
     & \makebox[\maxblocklabelsize]{\hfill\blocklabel{blocknum}}\\
  \end{tabular}
  \vspace{\belowcaptionskip}
  \setlength{\parindent}{\parindentsave}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\endinput