Skip to content

Commit

Permalink
Added \preparematrix description, fixed matlab usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
romeovs committed Feb 11, 2012
1 parent 15d9c93 commit e0f5ac7
Showing 1 changed file with 56 additions and 33 deletions.
89 changes: 56 additions & 33 deletions mattex_en.tex
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ \subsection{Setting variable values using \texttt{Mset}}
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 |]|.
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
Expand Down Expand Up @@ -220,10 +220,10 @@ \subsection{Matrices}
Te generate quick matrices use the following commands:

\bigskip
{\co|\makematrix|\ARGU{name}\ARGU{N}\ARGU{M}}
{\co|\preparematrix|\ARGU{name}\ARGU{N}\ARGU{M}}\mrg{preparematrix}\\
{\co|\usematrix|}\mrg{usematrix}

\mrg{makematrix}
This builds an $N$ by $M$ matrix of the variables stored in the name \argu{name}. To make a two by two matrix called |A| one would do:
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}
Expand All @@ -233,13 +233,14 @@ \subsection{Matrices}
\end{texcode}
\end{center}

to use this matrix one then would do:
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}
\begin{tabular}{NM}
\preparematrixmatrix{A}{2}{2}
\begin{tabular}{cc}
\noheader
\hline
\makematrix{A}{2}{2}
\usematrix
\hline
\end{tabular}
\end{texcode}
Expand All @@ -250,24 +251,47 @@ \subsection{Matrices}
\centering
\begin{tabular}{cc}
\hline
1 & \SI{2+-0.2}{} \\
3 & \SI{4+-0.4}{} \\
A(1,1) & A(1,2) \\
A(2,1) & A(2,2) \\
\hline
\end{tabular}
\end{table}

There are two column types defined by \mt:
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 |N|: sets every cell of that column in an |\Mval| command.
\item |M|: sets every cell of that column in an |\Mnum| command.
\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}

\bigskip
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 |\Mval| or |\Mnum| commands even when the |N| or |M| columns are used.

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|}
Expand All @@ -276,7 +300,7 @@ \subsection{Matrices}
Stops the header in a |tabular| environment. For example:
\begin{center}
\begin{texcode}
\begin{tabular}{NM}
\begin{tabular}{vn}
\hline \header
Hdr 1 & Hdr 2 \noheader \\
\hline
Expand All @@ -299,11 +323,8 @@ \subsection{Matrices}
\end{tabular}
\end{table}

|\header| and |\noheader|, of course, can also be used for footer or intermediary rows.


%% Examples
\newpage
\subsection{Examples}
This example shows how the output is when all information is set for a variable:

Expand Down Expand Up @@ -425,35 +446,35 @@ \subsection{Requirements}

% 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 are described in this section.
With \mt come several matlab scripts that export variables in the correct format, so that \mt can pick them up.

The scripts were initially written in matlab and then tested in octave, so they should work on both. Recently, however, I've stopped using matlab myself and have switched to octave completely. However, since I come from a matlab history, 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 in makefiles etc. This makes the combination latex-mattex-octave extremely powerful. What follows is applicable to octave as well as to matlab.
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 value has de same meaningful digits.
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. Overwrites the default append behaviour.
\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|: turns on the exponent behaviour. This causes number 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 value will change accordingly).
\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 (in random order). For instance~|'wse4'| will write the values using 4 significant digits. (thereby clearing the file first), not print any additional information and use scientific notation, even when the magnitude is -1, 0 or 1.
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 (this is, scalars or vectors of size $\leq2$) and writes them. The same options as for |writevars| apply.
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}|)|}
Expand All @@ -463,7 +484,7 @@ \section{Matlab usage}

|\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 |\makematrix|.
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
Expand All @@ -480,17 +501,17 @@ \section{Recommended workflow}
d = exp(c)*100000;
\end{matlabcode}
\end{center}
\item Export the variables to a file |vars.tex|, using one of the above matlab m-files. Do this by adding:
\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}
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{f}{32.5}{1.9}{5}
\Mset{d}{32.5}{1.9}{5}
\end{texcode}
\end{center}
\item Input the file by putting |\input{file.tex}| in the \TeX document.
Expand All @@ -499,7 +520,7 @@ \section{Recommended workflow}
\begin{texcode}
\input{file.tex}
\Mnum{c}
\MSI{f}{\metre}
\MSI{d}{\metre}
\end{texcode}
\end{center}

Expand All @@ -509,9 +530,11 @@ \section{Recommended workflow}
$\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.
Expand Down

0 comments on commit e0f5ac7

Please sign in to comment.