summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/readarray/readarray.sty
blob: ccdc9a7cb361c8bff6392d9fe253b9e98314ad33 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
\ProvidesPackage{readarray}
[2013/04/02 v1.01
Routines for inputting array data and recalling it on an 
element-by-element basis.  Currently supports 2-D and 3-D array]
% 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.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Steven B. Segletes.
%
% Revisions:
% v1.01 Documentation revision
\usepackage{ifthen}
\usepackage{ifnextok}
%
\newcounter{@index}
\newcounter{@plane}
\newcounter{@row}
\newcounter{@col}
\newcounter{use@args}
\def\the@rule{\rule{.8ex}{1.6ex}}%
%
\newcommand\readArrayijk[4]{%
  \getargsC{#1}%
  \setcounter{@plane}{\numexpr(\narg/#4/#3)}%
  \setcounter{use@args}{\numexpr\arabic{@plane}*#3*#4}%
  \ifthenelse{\arabic{use@args} > \narg}{%
    \addtocounter{@plane}{-1}%
    \setcounter{use@args}{\numexpr\arabic{@plane}*#3*#4}%
  }{}%
  \expandafter\edef\csname#2PLANES\endcsname{\arabic{@plane}}%
  \expandafter\edef\csname#2ROWS\endcsname{#3}%
  \expandafter\edef\csname#2COLS\endcsname{#4}%
  \expandafter\edef\csname#2CELLS\endcsname{\arabic{use@args}}%
  \setcounter{@index}{0}%
  \setcounter{@plane}{1}%
  \setcounter{@row}{1}%
  \setcounter{@col}{0}%
  \whiledo{\value{@index} < \value{use@args}}{%
    \addtocounter{@index}{1}%
    \addtocounter{@col}{1}%
    \ifthenelse{\value{@col} > #4}%
      {\addtocounter{@row}{1}%
       \addtocounter{@col}{-#4}}%
      {}%
    \ifthenelse{\value{@row} > #3}%
      {\addtocounter{@plane}{1}%
       \addtocounter{@row}{-#3}}%
      {}%
    \expandafter\protected@edef%
         \csname#2X\roman{@plane}X\roman{@row}X\roman{@col}\endcsname%
      {\expandafter\csname arg\roman{@index}\endcsname}%
  }%
}
%
\newcommand\readArrayij[3]{%
  \getargsC{#1}%
  \setcounter{@row}{\numexpr(\narg/#3)}%
  \setcounter{use@args}{\numexpr\arabic{@row}*#3}%
  \ifthenelse{\arabic{use@args} > \narg}{%
    \addtocounter{@row}{-1}%
    \setcounter{use@args}{\numexpr\arabic{@row}*#3}%
  }{}%
  \expandafter\edef\csname#2ROWS\endcsname{\arabic{@row}}%
  \expandafter\edef\csname#2COLS\endcsname{#3}%
  \expandafter\edef\csname#2CELLS\endcsname{\arabic{use@args}}%
  \setcounter{@index}{0}%
  \setcounter{@row}{1}%
  \setcounter{@col}{0}%
  \whiledo{\value{@index} < \value{use@args}}{%
    \addtocounter{@index}{1}%
    \addtocounter{@col}{1}%
    \ifthenelse{\value{@col} > #3}%
      {\addtocounter{@row}{1}%
       \addtocounter{@col}{-#3}}%
      {}%
    \expandafter\protected@edef%
                   \csname#2X\roman{@row}X\roman{@col}\endcsname%
      {\expandafter\csname arg\roman{@index}\endcsname}%
  }%
}
%
\def\nonposmessage{{\tiny Only positive array indices permitted}}
\def\planemessage#1#2{{\tiny PLANE=#2 exceeds limit for #1}}
\def\rowmessage#1#2{{\tiny ROW=#2 exceeds limit for #1}}
\def\colmessage#1#2{{\tiny COL=#2 exceeds limit for #1}}
%
\newcommand\Arrayijk[5][$]{%
  \setcounter{@plane}{#3}%
  \setcounter{@row}{#4}%
  \setcounter{@col}{#5}%
  \ifthenelse{\value{@plane}<1 \OR \value{@row}<1 \OR \value{@col}<1}{%
    \if$#1\nonposmessage\else#1\fi}{%
    \ifthenelse{#3 > \csname#2PLANES\endcsname}{%
      \if$#1\planemessage{#2}{#3}\else#1\fi}{%
      \ifthenelse{#4 > \csname#2ROWS\endcsname}{%
        \if$#1\rowmessage{#2}{#4}\else#1\fi}{%
        \ifthenelse{#5 > \csname#2COLS\endcsname}{%
          \if$#1\colmessage{#2}{#5}\else#1\fi}{%
          \csname#2X\roman{@plane}X\roman{@row}X\roman{@col}\endcsname}%
        }%
      }%
  }%
}
%
\newcommand\Arrayij[4][$]{%
  \setcounter{@row}{#3}%
  \setcounter{@col}{#4}%
  \ifthenelse{\value{@row}<1 \OR \value{@col}<1}{%
    \if$#1\nonposmessage\else#1\fi}{%
    \ifthenelse{#3 > \csname#2ROWS\endcsname}{%
      \if$#1\rowmessage{#2}{#3}\else#1\fi}{%
      \ifthenelse{#4 > \csname#2COLS\endcsname}{%
        \if$#1\colmessage{#2}{#4}\else#1\fi}{%
        \csname#2X\roman{@row}X\roman{@col}\endcsname}%
      }%
  }%
}
%
\newcommand\readdef[2]{%
\def\first@row{T}%
\def\first@plane{T}%
\catcode\endlinechar=10\relax%
\def#2{}%
\newread\file%
\openin\file=#1%
\loop\unless\ifeof\file%
    \read\file to\fileline % Reads a line of the file into \fileline%
    \protected@edef#2{#2\fileline}%
    \if T\first@row\getargsC{#2}\setcounter{@col}{\numexpr(\narg-1)}%
      \edef\ncols{\arabic{@col}}\def\first@row{F}\setcounter{@row}{1}%
    \else%
      \if T\first@plane\getargsC{\fileline}\ifthenelse{\equal{\narg}{1}}{%
        \edef\nrows{\arabic{@row}}\def\first@plane{F}}{%
        \addtocounter{@row}{1}}%
      \fi
    \fi%
\repeat%
\closein\file%
\catcode\endlinechar=5\relax%
}
%
\newcommand\showargs[1][0]{%
  narg=\narg:~%
  \the@rule%
  \setcounter{arg@index}{0}%
  \setcounter{break@count}{0}%
  \whiledo{\value{arg@index} < \narg}{%
    \addtocounter{arg@index}{1}%
    \addtocounter{break@count}{1}%
    \csname arg\roman{arg@index}\endcsname%
    \the@rule%
    \ifthenelse{\equal{#1}{\value{break@count}}}{%
      \ifthenelse{\equal{\value{arg@index}}{\narg}}{}{%
        \newline\the@rule\addtocounter{break@count}{-#1}}%
      }%
    {}%
  }%
  \setcounter{arg@index}{0}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The \getargsC macro mimics the behavior of the \getargs macro
% of the stringstrings package, but runs faster, and can handle
% arbitrary tokens.  For the development of \getargsC, significant
% assistance was provided by David Carlisle, for which the author is
% most appreciative.
% http://tex.stackexchange.com/questions/101604/
%      parsing-strings-containing-diacritical-marks-macros
%
\def\string@end{$\SaveHardspace}
\def\converttilde{F}
\newcounter{arg@index}
\newcounter{break@count}
\let\SaveHardspace~%%%
%
\def\getargsC#1{%
  \if T\converttilde\def~{ }\else\catcode`~=12\fi
  \protected@edef\the@string{#1}%
  \setcounter{arg@index}{0}%
  \lowercase{\expandafter\parse@Block\the@string} \string@end
  \let~\SaveHardspace%
  \catcode`~=13
}
%
\def\parse@Block#1 {%
  \stepcounter{arg@index}%
  \@namedef{arg\roman{arg@index}}{#1}%
  \futurelet\tmp\parse@Block@}
%
\def\parse@Block@{%
\ifx\tmp\string@end\edef\narg{\thearg@index}\expandafter\@gobble
\else\expandafter\parse@Block\fi}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\endinput