summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/mattex
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /macros/latex/contrib/mattex
Initial commit
Diffstat (limited to 'macros/latex/contrib/mattex')
-rw-r--r--macros/latex/contrib/mattex/formatvars.m131
-rw-r--r--macros/latex/contrib/mattex/mattex.sty186
-rw-r--r--macros/latex/contrib/mattex/mattex_en.pdfbin0 -> 322843 bytes
-rw-r--r--macros/latex/contrib/mattex/mattex_en.tex546
-rw-r--r--macros/latex/contrib/mattex/parsemopts.m98
-rw-r--r--macros/latex/contrib/mattex/writeallvars.m79
-rw-r--r--macros/latex/contrib/mattex/writemat.m133
-rw-r--r--macros/latex/contrib/mattex/writevars.m187
8 files changed, 1360 insertions, 0 deletions
diff --git a/macros/latex/contrib/mattex/formatvars.m b/macros/latex/contrib/mattex/formatvars.m
new file mode 100644
index 0000000000..d3034b29da
--- /dev/null
+++ b/macros/latex/contrib/mattex/formatvars.m
@@ -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
+
diff --git a/macros/latex/contrib/mattex/mattex.sty b/macros/latex/contrib/mattex/mattex.sty
new file mode 100644
index 0000000000..e06261d0d9
--- /dev/null
+++ b/macros/latex/contrib/mattex/mattex.sty
@@ -0,0 +1,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
diff --git a/macros/latex/contrib/mattex/mattex_en.pdf b/macros/latex/contrib/mattex/mattex_en.pdf
new file mode 100644
index 0000000000..38ee7c289b
--- /dev/null
+++ b/macros/latex/contrib/mattex/mattex_en.pdf
Binary files differ
diff --git a/macros/latex/contrib/mattex/mattex_en.tex b/macros/latex/contrib/mattex/mattex_en.tex
new file mode 100644
index 0000000000..f38993979f
--- /dev/null
+++ b/macros/latex/contrib/mattex/mattex_en.tex
@@ -0,0 +1,546 @@
+% PREAMBLE
+
+% PACKAGE LOADING
+\documentclass[a4paper,10pt]{article}
+\usepackage[utf8]{inputenc}
+\usepackage[T1]{fontenc}
+\usepackage{hyperref}
+\usepackage{verbatim}
+\usepackage{minted}
+\usepackage{fancyvrb}
+\usepackage{color}
+\usepackage{siunitx}
+\usepackage{parskip}
+\usepackage[english]{babel}
+\usepackage[babel]{microtype}
+\usepackage{makeidx}
+
+
+% PACKAGE OPTIONS
+\sisetup{
+ separate-uncertainty = true,
+}
+
+\usemintedstyle{bw}
+\renewcommand{\theFancyVerbLine}{\textcolor[rgb]{0.4,0.4,0.4}{\scriptsize{\arabic{FancyVerbLine}}}}
+
+\definecolor{bg}{rgb}{0.95,0.95,0.95}
+\newminted{tex}{linenos=none,mathescape,bgcolor=bg,gobble=2}
+\newminted{matlab}{linenos=none,mathescape,gobble=2,bgcolor=bg}
+\DefineShortVerb{\|}
+\makeindex
+
+
+% META
+\author{Romeo Van Snick\\
+\href{mailto:romeovs@gmail.com}{\nolinkurl{romeovs@gmail.com}}}
+\date{\today}
+\title{The \mt package}
+
+
+% MACRO's
+\newcommand{\mt}{Mat\TeX\ }
+
+\newcommand\argu[1]{{\color{black}$\langle$\textit{#1}$\rangle$}}
+\newcommand\ARGU[1]{\texttt{\{}\argu{#1}\texttt{\}}}
+\newcommand\co[0]{\color{violet}}
+
+\newcommand\mtmrg[1]{\marginpar{\texttt{#1}}}
+\newcommand\mmrg[1]{\index{#1@\texttt{#1}}\mtmrg{#1}}
+\newcommand\mrg[1]{\index{#1@\texttt{\textbackslash #1}}\mtmrg{\textbackslash #1}}
+\renewcommand{\labelitemi}{\( \cdot \)}
+
+
+
+\begin{document}
+\maketitle
+
+% INTRO
+\mt is a rudimentary set of \LaTeX\ macros and matlab scripts that try to make your life easier when you are working with calculated matlab or octave numerical values in a \LaTeX\ document.
+
+In stead of copy-pasting the calculated values into the latex document, it is possible to export them to an intermediary file that can be |\input|'ed into the document. Saving you from having to do the job twice if you changed somethng in the calculations and get a different numerical result.
+
+In what follows I'll try to desccribe how to use the \LaTeX\ macro's as well as the matlab scripts that can be used to save the values of you calculated values. But first, a very short introduction into the nomenclature of the scientific notation.
+
+
+% ABOUT NUMBERS
+\section{About numbers}
+The standard (as far as I know) way to scientifically write a number is generally:
+\[
+ (x \pm s_x) \times 10 ^{e}
+\]
+In this notation several parts are distinguised:
+\begin{itemize}
+ \item the significand \( x \)
+ \item the uncertianty estimate or error \( s_x \)
+ \item the exponential \( e \)
+\end{itemize}
+Usually the uncertainty estimate is rounded off to two \emph{significant digits}. The significand is then rounded to match the rounding of the uncertainty estimate, so that the last two significant digits match the order of magnitude of those of the uncertainty estimate. The exponent is then adapted so that the order of magnitude of the total is still correct.
+
+Please visit \url{http://www.chemteam.info/SigFigs/SigFigs.html} for a more in depth discussion on the scientific notation.
+
+I will use the above nomenclature to denote the parts of a number. A number, consisting of all of the above parts (or a subset of them), will be denoted by the term \emph{value}. The name you give this number (i.e. the place you store it in) will be called a \emph{variable}.
+
+So if you would type:
+\begin{center}
+ \begin{matlabcode*}{linenos=none}
+ >> a = 14e3;
+ \end{matlabcode*}
+\end{center}
+into a matlab or octave prompt, you will have created a variable called |a| with |value| that is |14e3|. This value is compromised of a significand |14| and an exponent |3|.
+
+To have a variable with a value that also has an error estimate you should type\footnote{This of couse can be done in other ways, yet is the necessary method to add variables and their error in the matlab scripts that join the \mt package.}:
+\begin{center}
+ \begin{matlabcode*}{linenos=none}
+ >> a = [21.3 1.1]*1e3;
+ \end{matlabcode*}
+\end{center}
+To get a value that has a significand |12.1|, an exponent |3| and an uncertainty estimate |1.1|.
+
+
+
+% LATEX USAGE
+\section{\LaTeX\ usage}
+This section will discuss the correct usage of the \LaTeX\ macro's that are supplied in this package.
+
+To gain acces to the macro's put the following in the preamble of your document:
+\begin{center}
+ \begin{texcode*}{linenos=none}
+ \usepackage{mattex}
+ \end{texcode*}
+\end{center}
+This will allow you to use the macro's explained in the following sections.
+
+%% setting values
+\subsection{Setting variable values using \texttt{Mset}}
+To set a variable, you can use following syntax:
+
+{\co|\Mset|\ARGU{name}\ARGU{significand}\ARGU{error}\ARGU{exponent}}
+
+\mrg{Mset}
+This will set the variable named \argu{name} to:
+\[
+ (\text{\argu{significand}} \pm \text{\argu{error}}) \times 10^{\text{\argu{exponent}}}
+\]
+Setting a variable overwrites it's previous value (if it already had a value).
+
+Some examples:
+
+\begin{minipage}[t]{0.45\textwidth}
+ \begin{texcode}
+ \Mset{a}{12}
+ \Mset{b}{123}{24}
+ \Mset{c}{34}{}{-5}
+ \Mset{d}{72}{11}{-9}
+ \end{texcode}
+\end{minipage}\hfil
+\begin{minipage}[c]{0.50\textwidth}
+\vspace{9pt}
+set the variable |a| to \num{12}\\
+set the variable |b| to \SI{123+-14}{}\\
+set the variable |c| to \SI{34e-5}{}\\
+set the variable |d| to \SI{72+-11e-9}{}\\
+\end{minipage}
+
+The names can be almost anything, from alphabetic letters such as |a|, |b|, $\ldots$ to numbers |0|, |1|, $\ldots$ and multicharacter comdinations of these, e.g.\ |ab10|. Also, underscores, commas and braces are allowed so it's possible to make a variable that is called~|a_crit(2,3)|. The only thing I know of that is not allowed are, for obvious reasons, the tex active characters such as |\|, |{|, |}|, |[| and |]|. Note that most of these symbols cannot be used for argument names in matlab either, so this doesn't matter that much (if you don't understand this sentence, please ignore it and read on).
+
+
+%% getting values
+\subsection{Getting the values}
+Dependig on shoch parts of the value of a variable you need, you can use different macro's to get them (note that some knowledge of the |siunitx| package is advised while reading this section).
+
+In what follows we will assume a variable was set as follows:
+\begin{center}
+ \begin{texcode}
+ \Mset{e}{72}{11}{-9}
+ \end{texcode}
+\end{center}
+
+\bigskip
+{\co|\Mval|\ARGU{name}}
+
+\mrg{Mval}
+Gives you the significand and exponent of the variable \argu{name} as if put through the |\num| macro. So for instance |\Mval{e}| would be equal to typing~|\num{72e-9}|. The typeset result should be: $\num{72e-9}$.
+
+Here is were the reason d'\^{e}tre of this package lies. You could of course just have typed in |\num{72e-9}| in stead of going trough the hassle of first assigning this value to a variable and then getting it, yet this has a number of disadvantages compared to the \mt approach:
+\begin{itemize}
+ \item consistency: will the number you enter be entered consistenly troughout the document? Chances are you type a mistake or change notation in the middle of a document.
+ \item what if you made a mistake in the matlab calculations? You'd have to do a lot of work over again, editing each occurence of the variable in the document, possibly missing some occureces.
+ \item you really have to do the work of copy pasting tha value over from matlab yourself. This, to me is perhaps the biggest disadvantage. Using the mattex package, you can let your latex and matlab files coexist in a makefile and compile the whole bunch in one go, without the need of you interviening to copy-paste stuff over.
+\end{itemize}
+
+The name |\Mval| might seem a bit in contradiction to the nomenclature of this manual, but it makes sense: you only want the calculated \emph{value} of a variable\footnote{here value denotes significand with exponent.}.
+
+\bigskip
+{\co|\Merr|\ARGU{name}}
+
+\mrg{Merr}
+Gives you the same as |\Mval| but returns the uncertainty estimate instead, |\Merr{e}| results in~$\num{11}$.
+
+\bigskip
+{\co|\Mnum|\ARGU{name}}
+
+\mrg{Mnum}
+This gives you the full value of variable \argu{name}. |\Mnum{d}| would result in~$\SI{72+-11e-9}{}$.
+
+\bigskip
+{\co|\MSI|\ARGU{name}\ARGU{unit}}
+
+\mrg{MSI}
+This results in the same as |\Mnum| but allows you to append a unit specified by \argu{unit}, as you would do using the |\SI| command from the |siuntix| package. The rules that apply for the |siunitx| package appy here too as well as the settings you may have set for it. |\Mnum{d}{\metre}| will result in:~$\SI{72+-11e-9}{\metre}$. Please also read the |siunitx| package documentation.
+
+\textbf{Note:} For all of the above commands the same rule applies: if a certain part of a variable isn't set, it wont show up in any of these commands. So if we were to redifine our value |d| to |\Mval{72}{}{-9}| (i.e. without an error estimate set), |\Mval| would remain the same, |\Merr| would result in literally `nothing' and |\Mnum| would be equal to |\Mval|.
+
+
+%% internals
+\subsection{Internal macro's}
+You probably won't need the following macro's, yet they might come in handy if you want to define your own stuff based on what \mt macro's can do. The strings that come out of the following macro's can be passed on to the |siunitx| macro's for example (which is basically what is done in the macro's above).
+
+\bigskip
+{\co|\M|\ARGU{name}}
+
+\mrg{M}
+This gets the literal string that is saved under the variable with name \argu{name}. This macro returns a simple text string such as |72+-11e-9| or |12e-23|.
+
+\bigskip
+{\co|\Mvallit|\ARGU{name}}\mrg{Mvallit}\\
+Gives the literal string that is saved under the significand and exponent of variable \argu{name}. For |\Mset{e}{93}{9}{6}| this results in |93e9|.
+
+\bigskip
+{\co|\Merrlit|\ARGU{name}}
+
+\mrg{Merrlit}
+Gives the literal string that is saved under the error with exponent of variable \argu{name}. For |\Mset{f}{100}{10}{-9}| this results in~|10e-9|.
+
+
+%% Matrices
+\subsection{Matrices}
+Since commas and braces are allowed in the name of a variable, this can be used to make a matrix with indeces. There is also a matlab script that outputs matlab arrays to variables that \mt-readable (see |writemat|).
+
+Te generate quick matrices use the following commands:
+
+\bigskip
+{\co|\preparematrix|\ARGU{name}\ARGU{N}\ARGU{M}}\mrg{preparematrix}\\
+{\co|\usematrix|}\mrg{usematrix}
+
+Using the comidination of |\preparematrix| and |\usematrix| you can easily build a matrix. Say we have a 2 by 2 matrix called |A|. We set the elements using:
+\begin{center}
+ \begin{texcode}
+ \Mset{A(1,1)}{1}{0.1}
+ \Mset{A(1,2)}{2}{0.2}
+ \Mset{A(2,1)}{3}{0.3}
+ \Mset{A(2,2)}{4}{0.4}
+ \end{texcode}
+\end{center}
+
+If the elements are set using the above approach, we can prepare the matrix for use in a tabular environment using |\preparematrix|. This macro takes 3 argumentsr. The first argument \argu{name}, which in our case is |A|, the name of the matrix. The remaining arguments \argu{N} and \argu{M} tell |\preparematrix| that the size of the matrix is: \argu{N} is the number of rows and \argu{M} is the number of colums.
+\begin{center}
+ \begin{texcode}
+ \preparematrixmatrix{A}{2}{2}
+ \begin{tabular}{cc}
+ \noheader
+ \hline
+ \usematrix
+ \hline
+ \end{tabular}
+ \end{texcode}
+\end{center}
+
+should result in:
+\begin{table}[h]
+\centering
+ \begin{tabular}{cc}
+ \hline
+ A(1,1) & A(1,2) \\
+ A(2,1) & A(2,2) \\
+ \hline
+ \end{tabular}
+\end{table}
+
+Of course the above table isn't what we'd want to see, we need the values of the elements, not there names. To do this there are three column types defined by \mt:
+\begin{itemize}
+ \item |v|: sets every cell of that column in an |\Mval| command.
+ \item |n|: sets every cell of that column in an |\Mnum| command.
+ \item |e|: sets every cell of that column in an |\Merr| command.
+\end{itemize}
+
+This allows us to get the values of the table cells:
+\begin{center}
+ \begin{texcode}
+ \preparematrixmatrix{A}{2}{2}
+ \begin{tabular}{vn}
+ \noheader
+ \hline
+ \usematrix
+ \hline
+ \end{tabular}
+ \end{texcode}
+\end{center}
+would result in:
+\begin{table}[h]
+\centering
+ \begin{tabular}{cc}
+ \hline
+ \num{1} & \num{2+-0.2} \\
+ \num{3} & \num{4+-0.4} \\
+ \hline
+ \end{tabular}
+\end{table}
+
+Now there might be cells you don't want to have put trough the respective |\M...| command when using one of the above column types, header or footer rows for instance. This is where the |\header| and |\noheader| commands come in.
+{\co |\header|}
+
+\mrg{header}
+Starts the header in a |tabular| environment. The cells on each row after the |\header| macro are not put into the |\M...| commands even when the |v|, |n| or |e| columns are used.
+
+\bigskip
+{\co |\noheader|}
+
+\mrg{noheader}
+Stops the header in a |tabular| environment. For example:
+\begin{center}
+ \begin{texcode}
+ \begin{tabular}{vn}
+ \hline \header
+ Hdr 1 & Hdr 2 \noheader \\
+ \hline
+ \usematrix
+ \hline
+ \end{tabular}
+ \end{texcode}
+\end{center}
+
+Which should result in:
+\begin{table}[h]
+ \centering
+ \begin{tabular}{cc}
+ \hline
+ Hdr 1 & Hdr 2 \\
+ \hline
+ 1 & \SI{2+-0.2}{} \\
+ 3 & \SI{4+-0.4}{} \\
+ \hline
+ \end{tabular}
+\end{table}
+
+
+%% Examples
+\subsection{Examples}
+This example shows how the output is when all information is set for a variable:
+
+\begin{minipage}[t]{0.45\textwidth}
+ \begin{texcode}
+ \Mset{a}{123}{45}{6} % set a
+ \Mval{a}
+ \Merr{a}
+ \Mnum{a}
+ \MSI{a}{\kilo\gram}
+ \M{a}
+ \Mvallit{a}
+ \Merrlit{a}
+ \end{texcode}
+\end{minipage}\hfil
+\begin{minipage}[c]{0.5\textwidth}
+ \vspace{6.5pt}
+ $ $\\
+ $\num{123e6}$\\
+ $\num{45e6}$\\
+ $\SI{123+-45e6}{}$\\
+ $\SI{123+-45e6}{\kilo\metre}$\\
+ |123+-45e6|\\
+ |123e6|\\
+ |45e6|
+\end{minipage}
+\bigskip
+
+The next example shows how the output is when only the value is set for a variable:
+
+\begin{minipage}[t]{0.45\textwidth}
+ \begin{texcode}
+ \Mset{b}{78} % set b
+ \Mval{b}
+ \Merr{b}
+ \Mnum{b}
+ \MSI{b}{\kilo\gram}
+ \M{b}
+ \Mvallit{b}
+ \Merrlit{b}
+ \end{texcode}
+\end{minipage}\hfil
+\begin{minipage}[c]{0.5\textwidth}
+ \vspace{6.5pt}
+ $ $\\
+ $\num{78}$\\
+ $ $\\
+ $\SI{78}{}$\\
+ $\SI{78}{\kilo\metre}$\\
+ |78|\\
+ |78|\\
+
+\end{minipage}
+
+This example shows you how to make a nice table from some values:
+\begin{center}
+ \begin{texcode}
+ \Mset{c(1,1)}{12.1}{1.1}
+ \Mset{c(2,1)}{13.3}{1.0}
+ \Mset{c(3,1)}{11.2}{0.9}
+ \Mset{c(4,1)}{11.9}{1.3}
+ \Mset{c(5,1)}{12.5}{0.8}
+
+ \Mset{c(1,2)}{455}{14}
+ \Mset{c(2,2)}{457}{12}
+ \Mset{c(3,2)}{453.2}{7.3}
+ \Mset{c(4,2)}{455}{13}
+ \Mset{c(5,2)}{458}{12}
+
+ \makematrix{c}{5}{2}
+ \begin{table}[htp]
+ \centering
+ \begin{tabular}{MM}
+ \hline\header
+ Head 1 & Head 2 \noheader \\
+ \hline
+ \usematrix
+ \hline \header
+ Foot 1 & Foot 2\\
+ \hline
+ \end{tabular}
+ \end{table}
+ \end{texcode}
+\end{center}
+Should result in:
+\begin{table}[h]
+ \centering
+ \begin{tabular}{cc}
+ \hline
+ Head 1 & Head 2\\
+ \hline
+ \SI{12.1+-1.1}{} & \SI{455+-14}{}\\
+ \SI{13.3+-1.0}{} & \SI{457+-12}{}\\
+ \SI{11.2+-0.9}{} & \SI{453.2+-7.3}{}\\
+ \SI{11.9+-1.3}{} & \SI{455+-13}{}\\
+ \SI{12.5+-0.8}{} & \SI{458+-12}{}\\
+ \hline
+ Foot 1 & Foot 2\\
+ \hline
+ \end{tabular}
+\end{table}
+\vspace{-1cm}
+
+
+
+%% requirements
+\subsection{Requirements}
+Several packages are needed to be able to use \mt, they are listed below:
+\begin{itemize}
+ \item |pgfkeys|: this is used to set and save the variables.
+ \item |xstring|: used to compare strings.
+ \item |siunitx|: this one should be obvious. If you need this package, you'll probably have this loaded already.
+ \item |xparse|: for the flexible defining of commands.
+ \item |array|: for the defining of new column types in tabular.
+ \item |collcell|: also used for the special tabular cells.
+\end{itemize}
+
+
+
+% MATLAB USAGE
+\section{Matlab usage}
+With \mt come several matlab scripts that export variables in the correct format, so that \mt can pick them up.
+
+These scripts were initially written in matlab and then tested in octave, so they should work on both. Recently, however, I've stopped using matlab and have switched to octave completely. This, however, should pose no porblem since I come from a matlab background and my syntax hasn't changed, so I expect the scripts to work with matlab as well. The main reason why I switched to octave is that it works as a genuine compiler, so it can be used properly in makefiles etc. This makes the combination latex-mattex-octave extremely powerful. What follows is applicable to octave as well as to matlab.
+
+To use the scripts, make sure that the files |writevars.m|, |writeallvars.m|,\\ |formatvars.m|, |writemat.m| and |parsemopts.m| are in a directory that matlab can read.
+
+\bigskip
+{\co |writevars(|\argu{file}|,|\argu{opts}|,|\argu{var1}|,|\argu{var2}|,|$\ldots$|)|}
+
+\mmrg{writevars}
+This function can be used to write variables \argu{var1}, \argu{var2}, $\ldots$ to a file called~\argu{file}. The variables are automatically formatted is such a fashion that the error (if there is one) has got two significant digits and that the significand has corresponding meaningful digits.
+
+Through the optional argument \argu{opts} you can specify how the data should be written:
+\begin{itemize}
+ \item |a|: append the variables to the file. This is default behaviour so this option does not have to explicitly given.
+ \item |w|: write tto the file instead of appending. This clears the file before writing to it. This option overwrites the default append behaviour.
+ \item |s|: be silent. This causes writevars to refrain from writing information to the prompt and causes it not to write the datestring into the file. This is generally a good idea when writing from a loop.
+ \item |e|: force exponent. This causes numbers that have magnitude $-1$, $0$ or $1$ to be written with exponent (eg |1e-1| instead of |0.1| and |1e0| instead of |1|), which normaly would not be done.
+ \item |#|: any number greater than or equal to 1. This number denotes the number of significant digits that will be used for the error (the number of significant digits on the significand will change accordingly).
+\end{itemize}
+These options should be put in a |char| string (the order doesn't matter). For instance~|'wse4'| will write the values using 4 significant digits (clearing the file first), not print any additional information to the screen and use scientific notation, even when the magnitude is $-1$, $0$ or $1$.
+
+The variables \emph{must} be passed by name in matlab, and they will have the same name in the \LaTeX file as the name they were passed by. To give a value with an error pass a vector (also by name) containing the value and error.
+
+\bigskip
+{\co |writeallvars(|\argu{file}|,|\argu{opts}|)|}
+
+\mmrg{writeallvars}
+This basically does the same as |writevars| yet it searches the workspace for all the current variables that can be written (i.e. |double| and size $\leq2$) and writes them. The same options as for |writevars| apply.
+
+\bigskip
+{\co |writemat(|\argu{file}|,|\argu{matrix}|,|\argu{error matrix}|)|}
+
+\mmrg{writemat}
+Writes all the elements of the matlab array \argu{matrix} to the file specified in \argu{file}, with optional errors specified in the array \argu{error matrix}. After inputting \argu{file} has been input into \LaTeX the variables are available trough:
+
+|\Mval{|\argu{matrix}|(|\argu{i}|,|\argu{j}|)}|
+
+For example the value in the third row and second column of a matrix |A| is called by |\Mnum{a(3,2)}|. This matrix is of the correct form to be used in the |\preparematrix| approached described above.
+
+
+% RECOMMENDED WORKFLOW
+\section{Recommended workflow}
+To get the variables from a matlab session into a \LaTeX document I recommend using the following workflow:
+\begin{enumerate}
+ \item Do the calculations in matlab. You matlab script could look like this:
+\begin{center}
+ \begin{matlabcode}
+ a = [3.4 2.9 3.5 3.1 4.5];
+ b = mean(a);
+ s_b = std(a);
+ c = [b s_b];
+ d = exp(c)*100000;
+ \end{matlabcode}
+\end{center}
+ \item Export the variables to a file |vars.tex|, using one of the above matlab functions. Do this by adding:
+\begin{center}
+\begin{matlabcode}
+ writevars('file.tex','a',c,d);
+\end{matlabcode}
+\end{center}
+The file |file.tex| should look like this after you run your script:
+\begin{center}
+\begin{texcode}
+ \Mset{c}{3.48}{0.62}
+ \Mset{d}{32.5}{1.9}{5}
+\end{texcode}
+\end{center}
+ \item Input the file by putting |\input{file.tex}| in the \TeX document.
+ \item Use the variables defined in |file.tex| by using the macro's described above. For instance:
+\begin{center}
+\begin{texcode}
+ \input{file.tex}
+ \Mnum{c}
+ \MSI{d}{\metre}
+\end{texcode}
+\end{center}
+
+This should result in:
+
+ \begin{minipage}[t]{\textwidth}
+ $\SI{3.48+-0.62}{}$\\
+ $\SI{32.5+-1.9e5}{\metre}$
+ \end{minipage}
+
+\end{enumerate}
+
+
+
+% THANKS
+\section{Thanks!}
+A lot of credit goes out to Joseph Wright, the editor of the |siunitx| package. I've been a devote |siunitx| user for years now.
+
+I'm also in debt to the poeple at \url{http://tex.stackexchange.com/}, who do their best to answer every single one of my questions.
+
+
+\printindex
+\end{document}
diff --git a/macros/latex/contrib/mattex/parsemopts.m b/macros/latex/contrib/mattex/parsemopts.m
new file mode 100644
index 0000000000..33b54a292e
--- /dev/null
+++ b/macros/latex/contrib/mattex/parsemopts.m
@@ -0,0 +1,98 @@
+function [ap,wr,sil,n,eg] = parsemopts(opts)
+ % [append, write, silent, n , eg] = PARSEMOPTS(opts)
+ % Returns which options were given in the opts char string.
+ % This is used in several of the mattex ;
+
+ % set default values
+ ap = 1;
+ wr = 0;
+ sil = 0;
+ n = 2;
+ eg = 0;
+
+
+ if strcmp(class(opts),'double')
+ % if only a number was given this must be n.
+ n = opts;
+ elseif strcmp(class(opts),'char')
+
+ w = strfind(opts,'w'); % write ?
+ a = strfind(opts,'a'); % append ?
+ s = strfind(opts,'s'); % silent ?
+ e = strfind(opts,'e'); % scientific ?
+
+ % error if both write and append are given, default append if nothing is given
+ if ( (numel(a) > 0) && ( numel(w) > 0 ) )
+ error('both the append and write options were given, this is contradictory');
+ elseif (numel(a) == 0) && ( numel(w) == 0 )
+ append = 1;
+ write = 0;
+ end
+
+ % write
+ if numel(w) > 0
+ ap = 0;
+ wr = 1;
+ opts(w) = ' ';
+ end
+
+ % append
+ if numel(a) > 0
+ ap = 1;
+ wr = 0;
+ opts(a) = ' ';
+ end
+
+ % silent
+ if numel(s) > 0
+ sil = 1;
+ opts(s) = ' ';
+ else
+ sil = 0;
+ end
+
+ % scientific
+ if numel(e) > 0
+ eg = 1;
+ opts(e)=' ';
+ else
+ eg = 0;
+ end
+
+ % find numerical characters in string
+ id = regexp(opts,'\d');
+
+ if length(id) == 0
+ n = 2;
+ else
+ % check if there is a gap in id (this would be bad, two numbers are given)
+ bad = 0;
+ for i = id(1):id(end)
+ if sum(i==id) == 0
+ bad = bad + 1;
+ end
+ end
+ if bad > 0
+ error(['there are multiple numbers given in the options string, I expected'...
+ ' only one.']);
+ else
+ numstr = opts(id);
+ n = str2num(numstr);
+ opts(id) = '';
+ end
+ end
+
+ opts(strfind(opts,' '))=''; % clear remaining whitespace
+
+ % are any unknown options given?
+ if numel(opts)>0
+ error(['unknown option: ' opts]);
+ end
+ end
+
+ % check if value of n is erranous
+ if n < 1
+ error([ 'n must be absolutely positive (n>0). ' ...
+ 'You cannot have 0 or less significant digits.']);
+ end
+end
diff --git a/macros/latex/contrib/mattex/writeallvars.m b/macros/latex/contrib/mattex/writeallvars.m
new file mode 100644
index 0000000000..b780056b46
--- /dev/null
+++ b/macros/latex/contrib/mattex/writeallvars.m
@@ -0,0 +1,79 @@
+function writeallvars(file,option)
+ % WRITEALLVARS(file,option) writes all suitable variables in the workspace to the file
+ % specified in file using writevars options 'option'.
+ % The variables are suitable when their size is equal to [1,1], [1,2] or [2,1] an when
+ % they are of the 'double' class.
+ %
+ % See also WRITEVARS, FORMATVARS, WRITEMAT
+
+ global var_names
+ global var_names_l
+ global temp
+ evalin('caller',[ 'global var_names;',...
+ 'var_names = who;',...
+ 'global var_names_l;',...
+ 'var_names_l=length(var_names);',...
+ 'global temp;',...
+ ]);
+
+ if var_names_l == 0
+ warning('there were zero variables in workspace, nothing was done');
+ else
+ disp([num2str(var_names_l-1) ' variables were found...']);
+
+ writestring = [];
+ good = 0;
+ bad = 0;
+ bad_names={};
+ for i = 1:var_names_l
+ if ~strcmp(var_names{i},'var_names')
+ evalin('caller', ['temp = ' char(var_names{i}) ';']);
+
+ if numel(temp)>=1 && numel(temp)<=2 && strcmp(class(temp),'double')
+ writestring = [writestring ',' char(var_names{i})];
+ good = good + 1;
+ else
+ bad = bad +1;
+ bad_names{end+1} = var_names{i};
+ end
+ end
+ end
+
+ if good > 0
+ disp([num2str(good) ' of which are writable, ' num2str(bad) ' of which are not.']);
+ fprintf('\n');
+ try
+ evalin('caller', [ 'writevars(''' file ''',''' option '''' writestring ');']);
+ catch
+ evalin('caller',[ 'clear var_names var_names_l temp' ]);
+ error('there was an error in writevars, probably a bad option or filename');
+ end
+ if strcmp(option,'as')
+ disp(['silent appending variables to ''' file '''']);
+ elseif strcmp(option,'ws')
+ disp(['silent writing variables to ''' file '''']);
+ end
+
+ switch bad
+ case 0
+
+ case 1
+ fprintf('the unwritten variable is:\n')
+ fprintf(['\t' char(bad_names{1}) '\n'])
+ otherwise
+ if bad < 5
+ fprintf('the unwritten variables are:\n')
+ for i = 1:length(bad_names)
+ fprintf(['\t' char(bad_names{i}) '\n'])
+ end
+ end
+ end
+
+ else
+ warning([ 'none of the found variables were suitable for writing,'...
+ 'make sure there are\n some variables that are 1x1 or 1x2']);
+ end
+
+ evalin('caller',[ 'clear var_names var_names_l temp' ]);
+
+end
diff --git a/macros/latex/contrib/mattex/writemat.m b/macros/latex/contrib/mattex/writemat.m
new file mode 100644
index 0000000000..a556023d91
--- /dev/null
+++ b/macros/latex/contrib/mattex/writemat.m
@@ -0,0 +1,133 @@
+function writemat(file,varargin)
+ % WRITEMAT(file,opts,A,s_A) writes the matrix to mat-tex variables to file.
+ % This can be included using in latex using the mat-tex commands.
+ % eg.
+ % A = [ 1, 2; 3, 4]*1e-9;
+ % s_A = [ 0.1, 0.2; 0.3, 0.4]*1e-9;
+ % writemat('example.txt',A,s_A);
+ %
+ % will result in a file 'example.txt' that contains
+ %
+ % \Mset{A(1,1)}{1}{0.1}{-9}
+ % \Mset{A(1,2)}{2}{0.2}{-9}
+ % \Mset{A(2,1)}{3}{0.3}{-9}
+ % \Mset{A(2,2)}{4}{0.4}{-9}
+ %
+ % which can be included in a tex file. Then use
+ %
+ % \makematrix{A}{2}{2}
+ % \begin{tabular}{MM}
+ % \usematrix
+ % \end{tabular}
+ %
+ % to typeset the table.
+ %
+ % The optional options argument can be a char string that contains in random order any
+ % of these options:
+ %
+ % a: append, this is default
+ % w: write to the file in stead of appending to it. This will clear the file.
+ % s: do not write timestamps into the file and don't be verbose on screen (good when
+ % writing from a loop.
+ % e: always use exponential notation (see formatvars).
+ % #: replace this by any number. It will cause # significant digits to be used instead
+ % of just 2.
+ %
+ % See also WRITEVARS, FORMATVARS, WRITEALLVARS
+
+
+ appendstr = [ '%%-- ' datestr(now) ' --%%\n'];
+
+ % check if options were given.
+ if strcmp(class(varargin{1}),'char')
+ opts=varargin{1};
+ [append, write, silent, n, e_given] = parsemopts(opts);
+ o = 1;
+ else
+ [append, write, silent, n, e_given] = parsemopts('');
+ o = 0;
+ end
+
+ if append
+ [FID] = fopen(file,'a');
+ if ~silent
+ fprintf(FID,appendstr);
+ disp(['appending variables to ' file])
+ end
+ elseif write
+ [FID] = fopen(file,'w');
+ if ~silent
+ fprintf(FID,appendstr);
+ disp(['writing variables to ' file])
+ end
+ end
+
+ if e_given
+ e = 'e';
+ else
+ e = '';
+ end
+
+ if numel(varargin) - o == 2
+ a = varargin{1+o};
+ s_a = varargin{2+o};
+ str = inputname(2+o);
+
+ if size(a)==size(s_a)
+ [I,J] = size(a);
+ for i = 1:I
+ for j = 1:J
+ [A,s_A] = formatvars(a(i,j),s_a(i,j),[num2str(n) e]);
+ eA = strfind(A,'e');
+ Aexp = '';
+ if size(eA) > 0
+ Aexp = A(eA+1:end);
+ A = A(1:eA-1);
+ end
+
+ es_A = strfind(s_A,'e');
+ if size(es_A) > 0
+ s_Aexp = s_A(es_A:end);
+ s_A = s_A(1:es_A-1);
+ end
+ posstr = ['(' num2str(i) ',' num2str(j) ')'];
+ defstr = [ '\\Mset{' str posstr '}{' A '}{' s_A '}{' Aexp '}\n' ];
+ fprintf(FID,defstr);
+ end
+ end
+
+ else
+ error('matrix sizes must coincide');
+ end
+
+ elseif numel(varargin) - o == 1
+ if n < 1
+ error('n cannot be smaller than 1. This would result in no significant digits.');
+ end
+ str = inputname(2+o);
+ a = varargin{1+o};
+ [I,J] = size(a);
+ for i = 1:I
+ for j=1:J
+ A = num2str(a(i,j),['%100.' num2str(n) 'g']); % get value as string
+ A(strfind(A,'+'))='';
+ eA = strfind(A,'e');
+ posstr = ['(' num2str(i) ',' num2str(j) ')'];
+ if size(eA) > 0
+ Aval = A(1:eA-1);
+ Aexp = A(eA+1:end);
+ defstr = [ '\\Mset{' str posstr '}{' Aval '}{}{' Aexp '}\n' ];
+ else
+ defstr = [ '\\Mset{' str posstr '}{' A '}\n' ];
+ end
+ fprintf(FID,defstr);
+ end
+ end
+
+ else
+ error('can only take one or two matrices.')
+ end
+
+ fclose(FID);
+
+end
diff --git a/macros/latex/contrib/mattex/writevars.m b/macros/latex/contrib/mattex/writevars.m
new file mode 100644
index 0000000000..a9d7fc45c1
--- /dev/null
+++ b/macros/latex/contrib/mattex/writevars.m
@@ -0,0 +1,187 @@
+function writevars(file, varargin)
+ % WRITEVARS(file,options,a,b,c,...) writes the variables a, b, c, etc. to the
+ % file specified in file for later use in a LaTeX document.
+ % The variable names will be given by their names
+ % when passed on to writevars. eg. matlab variable 'a' will be passed as '\a' in the tex file
+ %
+ % You can also use WRITEVARS to format variables with their corresponding error to the
+ % correct notation for use in physical reports eg.
+ %
+ % A = (101.3 +- 2.4)e4
+ %
+ % that is: give the error two significant digits and round the variable so that it's
+ % last two digits are of the same order of the error.
+ %
+ % To make use of this put the variable and it's error in a vector with the variables name:
+ %
+ % a = [101.299e10,2.423e10];
+ % writevars('file.txt',a);
+ %
+ % results in the 'file.txt' containing
+ %
+ % \Mset{a}{101.3}{2.4}{10}
+ %
+ % Use \input{file} in your tex file to gain acces to the variables, eg.
+ %
+ % \input{file.txt}
+ % value of a is \Mnum{a}.
+ %
+ % You cannot use temporary variables when using WRITEVARS. This would result in nameless
+ % variables in the tex file. So don't do:
+ %
+ % writevars('file.txt', [101.299, 2.423])
+ %
+ % You can use writevars to append variables to the file using the 'a' (append) option, which
+ % is also implied when no option is given. So both
+ %
+ % writevars('file.txt', 'a', a, b, c, ... );
+ %
+ % and
+ %
+ % writevars('file.txt', a, b, c, ...);
+ %
+ % will append the the variable definitions to the file. This is the smart thing to do since
+ % LaTeX will only use the last of the defined values, and this way you will have a history
+ % of all the values you have written. This behaviour can be turned off by using
+ % the 'w' option ('w' for write):
+ %
+ % writevars('file.txt','w', a, b, c,...);
+ %
+ % will empty the file before writing to it. This can be a good thing if the file is
+ % becoming overly long, thus slowing tex compilation.
+ %
+ % The other options that can be given are:
+ % a: append (this is default). Latex will use only the last values that were written.
+ % w: write instead of append. This clears the file before writing.
+ % s: silent, don't write information to console and refrain from writing a datestring
+ % to the file.
+ % e: passes the 'e' option to formatvars.
+ % #: replace by any number greater than 0. This tells formatvars which number
+ % of significant digits to use.
+ %
+ % you can give these options in random order in the optional options string. eg
+ %
+ % writevars(file,'wse4',a,b,c,...);
+ %
+ % silently writes the variables to the file with the 'e' option using 4 significant digits.
+ %
+ % See also WRITEALLVARS, WRITEMAT, FORMATVARS
+
+ appendstr = [ '%%-- ' datestr(now) ' --%%\n'];
+
+ % check if any variables were passed at all
+ if length(varargin) == 0
+ error('no variables were given');
+ end
+
+ % check if options string was given and if so, parse it using parsemopts.
+ switch class(varargin{1})
+ case 'char'
+ opts=varargin{1};
+ [append, write, silent, n, e_given] = parsemopts(opts);
+ s = 2;
+ case 'double'
+ [append, write, silent, n, e_given] = parsemopts('');
+ s = 1;
+ otherwise
+ error('Unexpected class in argument 2')
+ end;
+
+ if append
+ [FID] = fopen(file,'a');
+ if ~silent
+ fprintf(FID,appendstr);
+ disp(['appending variables to ' file])
+ end
+ elseif write
+ [FID] = fopen(file,'w');
+ if ~silent
+ fprintf(FID,appendstr);
+ disp(['writing variables to ' file])
+ end
+ end
+
+ if e_given
+ e = 'e';
+ else
+ e = '';
+ end
+
+ % loop variables in varargin
+ for i = s : length(varargin)
+
+ % get name of variable
+ str = inputname(i+1);
+
+ % check if this name is good or empty (temporary variable)
+ if length(str) == 0
+ error([ 'you cannot use temporary variables for they will ' ...
+ 'have no name on output. \nerror in input argument ' ...
+ num2str(i+1)]);
+ end
+
+ if ~strcmp(class(varargin{i}),'double')
+ error(['argument ' num2str(i+1) ' is not of class double']);
+ end
+
+ % check if variables is vector or single variables
+ if max(size(varargin{i})) == 2
+ % vector
+
+ vec = varargin{i}; % get the inputed vector
+ a = vec(1); % get first value
+ s_a = vec(2); % get second value
+
+ [A,s_A] = formatvars(a,s_a,[num2str(n) e]); % format the variables using formatvar
+ eA = strfind(A,'e');
+ Aexp = '';
+ if size(eA) > 0
+ Aexp = A(eA+1:end);
+ A = A(1:eA-1);
+ end
+
+ es_A = strfind(s_A,'e');
+ if size(es_A) > 0
+ s_Aexp = s_A(es_A:end);
+ s_A = s_A(1:es_A-1);
+ end
+ % defstr_s_a = [ '\\Mset{s_' str '}{' s_A '}\n' ];
+ defstr = [ '\\Mset{' str '}{' A '}{' s_A '}{' Aexp '}\n' ];
+
+ elseif size(varargin{i}) == [1 1]
+ % normal
+
+ if e_given
+ f=e;
+ else
+ f = 'g';
+ end
+ A = num2str(varargin{i},['%100.' num2str(n) f]); % get value as string
+
+ A(strfind(A,'+'))='';
+ eA = strfind(A,'e');
+ if size(eA) > 0
+ Aval = A(1:eA-1);
+ Aexp = A(eA+1:end);
+
+ % remove 0 in front of exponential
+ if Aexp(1) == '0'
+ Aexp(1) = '';
+ end
+ defstr = [ '\\Mset{' str '}{' Aval '}{}{' Aexp '}\n' ];
+ else
+ defstr = [ '\\Mset{' str '}{' A '}\n' ];
+ end
+ else
+ % unknown
+ error('variable must be of size [1 1] or [1 2]');
+
+ end
+ % print the string
+ fprintf(FID, defstr);
+ end
+
+ % close the file
+ fclose(FID);
+
+end