summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/mattex/mattex.sty
blob: e06261d0d92fccd879c527cae44d78be2e46ea38 (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
%% The MatTeX package
%% 
%% Copyright 2012 by Romeo Van Snick
%%
%% This file may be distributed and/or modified under the 
%% conditions of the LaTeX Project Publishing License, either
%% version 1.2 of this license ot any later version.
%% The latest version of this license is in:
%%
%%    http://www.latex-project.org/lppl.txt
%% 
%% and version 1.2 or later is part of all distributions of 
%% LaTeX version 1999/12/01 or later.
%% 
%% Matlab (c) is the property of its rightful owner.
%% 
%% package version: v0.1 2012/01/12
%%
%% email: romeovs@gmail.com
%%
%% note: this package is still in devolopment and I'm not a TeX guru, so errors will
%%       exist. Feel free to email me if a bug turns up.
%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mattex}[2012/01/12 v1.0 a set of macros to import matlab values]

% These are the required packages that need to be loaded
\RequirePackage{pgfkeys}
\RequirePackage{xstring}
\RequirePackage{siunitx}
\RequirePackage{xparse}
\RequirePackage{array}
\RequirePackage{collcell}
\ProcessOptions\relax


% PACKAGE OPTIONS

% Define the name for the pgfkeys directory where the matlab variable will be stored in
\newcommand{\mtdirectory}[0]{mtmatlab}

% PACKAGE CODE

% This command allows you to set a variable
\DeclareDocumentCommand{\Mset}{m m g g}
{
	% set the value (mandatory)
	\pgfkeyssetvalue{/\mtdirectory/#1/val}{#2}

	% set error if it is given
	\IfNoValueTF{#3}{
		% if nothing was given, literally set nothing, so variables can be properly overwritten
		\pgfkeyssetvalue{/\mtdirectory/#1/err}{}
		\pgfkeyssetvalue{/\mtdirectory/#1/pmstr}{}
	}{
		% if an argument was given, check if it was empty or not
		\IfEq{#3}{}{
			% empty: set empty values
			\pgfkeyssetvalue{/\mtdirectory/#1/err}{}
			\pgfkeyssetvalue{/\mtdirectory/#1/pmstr}{}
		}{
			% not empty: set values
			\pgfkeyssetvalue{/\mtdirectory/#1/err}{#3}
			\pgfkeyssetvalue{/\mtdirectory/#1/pmstr}{+-}
			}
	}

	% set exponent if it is given
	\IfNoValueTF{#4}{
		% if nothing was given, literally set nothing, so variables can be properly overwritten
		\pgfkeyssetvalue{/\mtdirectory/#1/exp}{}
		\pgfkeyssetvalue{/\mtdirectory/#1/estr}{}
	}{
		% if an argument was given, check if it was empty or not
		\IfEq{#4}{}{
			% empty: set all to empty
			\pgfkeyssetvalue{/\mtdirectory/#1/exp}{}
			\pgfkeyssetvalue{/\mtdirectory/#1/estr}{}
		}{
			% not empty: set values
			\pgfkeyssetvalue{/\mtdirectory/#1/exp}{#4}
			\pgfkeyssetvalue{/\mtdirectory/#1/estr}{e}
		}
	}

}

%% get string value
% this allows you the see which exact string is stored in the variable
\newcommand{\M}[1]
{%
	\pgfkeysvalueof{/\mtdirectory/#1/val}%
	\pgfkeysvalueof{/\mtdirectory/#1/pmstr}%
	\pgfkeysvalueof{/\mtdirectory/#1/err}%
	\pgfkeysvalueof{/\mtdirectory/#1/estr}%
	\pgfkeysvalueof{/\mtdirectory/#1/exp}%
}

%% get \SI
% allows you to use the variable as you would use \SI from siunitx
\newcommand{\MSI}[2]
{
	\SI{\M{#1}}{#2}
}

%% get error literal
\newcommand{\Merrlit}[1]{%
	\pgfkeysvalueof{/\mtdirectory/#1/err}%
	\pgfkeysvalueof{/\mtdirectory/#1/estr}%
	\pgfkeysvalueof{/\mtdirectory/#1/exp}%
}

%% get value literal
\newcommand{\Mvallit}[1]{%
	\pgfkeysvalueof{/\mtdirectory/#1/val}%
	\pgfkeysvalueof{/\mtdirectory/#1/estr}%
	\pgfkeysvalueof{/\mtdirectory/#1/exp}%
}

%% get value as \num
\newcommand{\Mval}[1]{
	\num{\Mvallit{#1}}
}

%% get error as \num
\newcommand{\Merr}[1]{
	% check if anything is in the err string and only try \num if there is
	\IfEq{\pgfkeysvalueof{/\mtdirectory/#1/err}}{}{}{
		\num{\Merrlit{#1}}
	}
}

%% get error and value (no units)
\newcommand{\Mnum}[1]{
	\num{\M{#1}}
}

%% Make and use matrices

\ExplSyntaxOn
\tl_new:N \l_romeo_cells_tl
\int_new:N \l_romeo_row_int
\int_new:N \l_romeo_col_int
\NewDocumentCommand { \preparematrix } { O{} m m m }
{
	\tl_set:Nn \l_romeo_cells_tl { }
	\int_set:Nn \l_romeo_row_int { #3 }
	\int_set:Nn \l_romeo_col_int { #4 }
	\prg_stepwise_inline:nnnn { 1 } { 1 } { \l_romeo_row_int }
	{
		\prg_stepwise_inline:nnnn { 1 } { 1 } { \l_romeo_col_int }
		{
			\int_compare:nF { ####1 = 1 } { \tl_put_right:Nn \l_romeo_cells_tl { & } }
			\tl_put_right:Nn \l_romeo_cells_tl
			{ #2 ( ##1 , ####1 ) }
		}
		\tl_put_right:Nn \l_romeo_cells_tl { \\ #1 }
	}
}
\DeclareExpandableDocumentCommand{ \usematrix } { }
{ \tl_use:N \l_romeo_cells_tl }
% written by egreg at stackexchange:
% http://tex.stackexchange.com/questions/40245/pgffor-and-the-alignment-character


%% tabular commands
% allows one to switch on/off header behaviour 
% if the header is on, M and N header types are ignored (just "c" columm type  applies)
\newtoggle{inTableHeader}	% Track if still in header of table
\toggletrue{inTableHeader}	% Set initial value
\newcommand*{\header}{\global\toggletrue{inTableHeader}}	% set header to true
\newcommand*{\noheader}{\global\togglefalse{inTableHeader}}	% set header to false

% define the table colums
% first define a new command that is sensitive to the inTableHeader toggle
\newcommand*{\tabMval}[1]{\iftoggle{inTableHeader}{#1}{\Mval{#1}}} 
\newcolumntype{v}{>{\collectcell\tabMval}c<{\endcollectcell}}

\newcommand*{\tabMnum}[1]{\iftoggle{inTableHeader}{#1}{\Mnum{#1}}}
\newcolumntype{n}{>{\collectcell\tabMnum}c<{\endcollectcell}}

\newcommand*{\tabMerr}[1]{\iftoggle{inTableHeader}{#1}{\Merr{#1}}}
\newcolumntype{e}{>{\collectcell\tabMerr}c<{\endcollectcell}}

%% end of file