Skip to content

Instantly share code, notes, and snippets.

@adhithyan15
Created July 22, 2012 20:50
Show Gist options
  • Save adhithyan15/3161024 to your computer and use it in GitHub Desktop.
Save adhithyan15/3161024 to your computer and use it in GitHub Desktop.
Converted mat2latex.m file
function output = mat2latexOctave(varargin)
%This function produces Latex code for any matlab matrix of any dimension.
%It requires the package amsmath in latex for successful operation. This
%code utilizes bmatrix environment.This code produces square brackets to
%enclose the elements of the matrix.This function has to be run as an
%argument for the builtin fprintf() function to work properly. For example
%this fprintf(mat2latex(A)).Decimal numbers are left as decimal numbers by default.
%If you want the decimal numbers to be formatted as rational numbers, pass
%in a second parameter 'ratio' with your matrix. For example,fprintf(mat2latex(A,'ratio'))
%
% Copyright (C) 2012 <Adhithya Rajasekaran,Renuka Rajasekaran,Sri Madhavi Rajasekaran>
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
varargin_size = size(varargin);%varargin is utilized for getting multiple
%versions of the function mat2latex().One version produces latex code in
%which decimal numbers are produced unchanged in latex. In the other
%version the user can force the decimal numbers to be changed into rational
%numbers.
if varargin_size(1,2) == 1%If only one parameter is passed in then the
%function will format decimal numbers as decimal numbers in latex.
input_matrix = varargin{1};
size_1 = size(input_matrix); % The size is required to set up looping constructs.
%Ignore all the red underlines. We are working on optimizing the code to
%avoid all the warning messages.
if isdecimal(input_matrix) == 0 % This condition checks whether the given matrix
%decimal numbers or not. The reason for this checking is if the given
%matrix doesn't contain any decimal numbers, then we can convert each
%element of the matrix into integers and then convert it to string to
%manipulate them. If we have decimal numbers then we use another string
%manipulation to convert decimal numbers. Read the documentation of
%isdecimal to know more about how we determine whether a matrix is decimal
%or not.
output = ['$\\begin{bmatrix} ' '']; %Beginning of latex bmatrix environment
for x = 1:size_1(1,1)
for y = 1:size_1(1,2)
intermediary_string = int16(input_matrix(x,y)); % Conversion to integers
intermediary_string = int2str(intermediary_string); %Integer to string conversion
intermediary_string = [intermediary_string '& ']; % Ampersand is added to denote the next element in a row
output = [output intermediary_string];
endfor
output = [output(1:end-2) ' '];% An additional ampersand and a space are inserted by the above for loop. So that is being removed here.
output = [output '\\\\'];% The two forward slashes indicate the end of a row.
endfor
output = [output(1:end-4) ' \\end{bmatrix}$\n\n']; % End of bmatrix environment.
else
output = ['$\\begin{bmatrix} ' '']; % Same as the above
for x = 1:size_1(1,1)
for y = 1:size_1(1,2)
if input_matrix(x,y) == int16(input_matrix(x,y)) % There is a possibility that within
%a matrix of decimal numbers, some numbers would be integers
%typecasted into decimals for convenience. For example,
%3.0000 is the same as the integer 3. So we check for that
%in the above condition.
intermediary_string = removeZeros(input_matrix(x,y));%If the condition is
%satisfied, we just extract the numbers before the decimal
%point.Read the documentation of removeZeros to know more
%about how we perform the operation.
intermediary_string = [intermediary_string '& '];% Same as above
output = [output intermediary_string];%Same as above
else
intermediary_string = removeZeros(input_matrix(x,y));% Same as above
intermediary_string = [intermediary_string '& '];%Same as above
output = [output intermediary_string];% Same as above
endif
endfor
output = [output(1:end-2) ' '];% Same as above
output = [output '\\\\'];%Same as above
endfor
output = [output(1:end-4) ' \\end{bmatrix}$\n\n'];%Same as above
endif
elseif varargin_size(1,2) == 2 %If two parameters are passed and the second
%parameter is a string 'ratio' then the decimal numbers inside the
%matrix are formatted into rational numbers.
if strcmpi(varargin{2},'ratio')
input_matrix = varargin{1};
output = ratioFormat(input_matrix);%Read more about ratioFormat in
%its documentation.
endif
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment