Skip to content

Commit

Permalink
Initial commit. This adds all the files I was working on before.
Browse files Browse the repository at this point in the history
A dexcription:

mattex.sty	the actual latex package
mattex_en.tex	the documentation source
README		a simple readme
parsemopts.m	an option parser to be used in the other .m scripts, it
		splits up an optin string into boolean values of
		possible options
formatvars.m	formats variables into strings with a certain structure
		to be used in the write[...] scripts
writevars.m	a script that allows one to write variables to a file
		in mattex.sty-readable format.
writallvars.m	does the same as above for all suitable variables in the
		workspace
writemat.m	allows one to write matrices
  • Loading branch information
romeovs committed Feb 10, 2012
0 parents commit 6feb22c
Show file tree
Hide file tree
Showing 8 changed files with 1,373 additions and 0 deletions.
54 changes: 54 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
----------------------------------------------------------------------
The MatTeX package = Integrating Matlab and TeX
Maintained by Romeo Van Snick
e-mail: [email protected]
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
keywords: matlab, siunitx, SI
----------------------------------------------------------------------

This package provides several macros that make it easier to save
and recall numerical variables, that have a value, an error and
an exponent (e.g. 123 +- 45 e 6) in a TeX document.

Macro's are supplied for quick access to these variables an to typeset them
according to the SI norms (using siuntix).

There is also a set of matlab m-files that allow you to export variables
from matlab in a format that is readable by TeX (using this package), with
the correct number of significant digits.

This makes it very easy to export the variables from matlab to an intermediary
file, import them in TeX and use their values.

Also, there are matlab functions supplied to export an array and there are TeX
macros that allow for typesetting these in a quick fashion.


Installation:
------------

The package is supplied in .sty format. It requires the following packages
to operate:

- pgfkeys
- xstring
- siuntixs
- xparse
- array
- collcell

TODO:
----

(*) parsemopts help.
(*) update manual to reflect recent (big) changes in the miles.

Release Notes:
-------------

Not released on CTAN (yet)!
This is a test version. Note that the project is still undergoing some big changes,
so usage might still change.

- added parsemopts.m for generalized options parsing
131 changes: 131 additions & 0 deletions formatvars.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
function [A,s_A] = formatvars(a, s, varargin)
% [A,s_A] = FORMATVARS(a,s_a,opts)
% assigns formatted strings to A and s_A that comply to the requirements of
% physics reports eg.
% - order of magnitude for the last two digits of A equal the order of magnitude of s_A
% - scientific notation
% the optional parameter n tells matlab how many significant digits the error s_A should
% have. The default is 2. eg.
%
% [A,s_A] = formatvars(123.456,3.567)
%
% results in: A = 123.4; s_A = 3.6; where A and s_A are both stings (not numbers!).
%
% The optional opts argument is a char string that contains on or more of the following
% options (in random order):
%
% e: if the 'e' option is given, formatvars will always use scientific notation,
% even when it normally wouldn't. The standard behaviour is that numbers of
% order -1, 0 or 1 (eg. 0.1, 1 and 10) would not be written in scientific notation.
% This can be turned of using the 'e' option.
% #: This can be any number greater than or equal to 1. It denotes the number of
% significant digits that formatvars will output (on the error value). For abvious
% reasons this must be greater than or equal to 1. If only this options need be given
% you can also pass it as a double (in stead of char string).
%
% eg, one would give an options string 'e3' to use 3 significant digits and make sure that
% scientific notation is always used.
%
% Known problems: if the number of significant digits you are trying to use is much larger
% (2 or more larger) this will result in rounding errors. This shouldn't be a problem,
% since this should never be the case in normal use.

% parse options
l = length(varargin);
switch l
case 0
% no options given
n = 2;
e_given = 0;
case 1
opts=varargin{1};
[append, write, silent, n, e_given] = parsemopts(opts);

% issue warning if unused option silent is given
if silent
warning('the silent option doesn''t apply here')
end

% issue warning if unused option write is given
if write
warning('the write option doesn''t apply here')
end

% issue warning if unused option append is given
if strcmp(class(opts),'char')
if size(strfind(opts,'a'))>0
warning('the append option doesn''t apply here')
end
end

otherwise
error('too many options strings were given.');
end


% order of magnitude of a must be greater than s
if abs(a/s) < 1
disp('order of magnitude of s_a cannot bigger than that of a');
end

% FORMAT S
m = floor(log10(abs(s))); % get the magnitude of the first significant digit of s

s_r = s/(10^m); % scale s to magnitude 1

RS = 10^(n-1); % round s_r to n significant digits
s_rr = round(s_r*RS)/RS;

% check if second significant digit of s is a zero
s_2 = s_rr - round(s_r*RS/10)/RS;
n_2 = floor(log10(abs(s_2)));
if n_2 < -1
% it is a zero
zerostr = '0';
dotstr = '.';
else
% it isn't a zero
zerostr = '';
dotstr = '';
end;

% FORMAT A
RA = 10 ^ (-m+n); % round a to the same order of magnitude as s_a
a_r = round(a*RA)/RA;

% special cases when magn(s_a) is equal to that of 10, 1 or 0.1
normal = 0;
switch m
case -1
% 0.1
A = [num2str(a_r, ['%20.' num2str(n) 'f'])];
s_A = [num2str(s_rr*10^m,['%#100.' num2str(n) 'g']) ];

case 0
% 1
A = [num2str(a_r,['%20.' num2str(n-1) 'f'])];
s_A = [num2str(s_rr*10^m,['%#100.' num2str(n) 'g']) ];

case 1
% 10
A = [num2str(a_r,['%20.' num2str(n-2) 'f'])];
s_A = [num2str(s_rr*10^m,['%#100.' num2str(n) 'g']) ];


otherwise
normal=1;
end

% use normal (scientific) behaviour
if normal || e_given
A = [num2str(a_r/(10^m),['%20.' num2str(n-1) 'f']) 'e' num2str(m)];
s_A = [num2str(s_rr, ['%20.' num2str(n-1) 'f']) zerostr 'e' num2str(m)];
end

% remove trailing dots
if (s_A(end) == '.')
s_A = s_A(1:end-1);
end

end

179 changes: 179 additions & 0 deletions mattex.sty
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
%% 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: [email protected]
%%
%% 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
\newtoks\romeotoks
\makeatletter
\newcommand{\makematrix}[3]{%
\global\romeotoks={\@gobble}%
\@tempcnta=\z@ \@tempcntb=\z@
\loop\ifnum\@tempcnta<#2
\advance\@tempcnta\@ne
{\loop\ifnum\@tempcntb<#3
\advance\@tempcntb\@ne
\edef\next{\unexpanded{#1}(\number\@tempcnta,\number\@tempcntb)}%
\global\romeotoks=\expandafter{\the\expandafter\romeotoks\expandafter&\next}%
\repeat}
\@tempcntb=\z@
\global\romeotoks=\expandafter{\the\romeotoks \\ \@gobble}%
\repeat
\global\romeotoks=\expandafter{\the\romeotoks &}%
}
\newcommand{\usematrix}{\the\romeotoks}
\makeatother
% 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{N}{>{\collectcell\tabMval}c<{\endcollectcell}}

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

%% end of file
Loading

0 comments on commit 6feb22c

Please sign in to comment.