help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: octave to (la)tex conversion


From: Leo Butler
Subject: Re: octave to (la)tex conversion
Date: Tue, 02 Oct 2012 11:26:59 -0400
User-agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/23.4 (gnu/linux)

Carnë Draug <address@hidden> writes:

> On 2 October 2012 01:01, Leo Butler <address@hidden> wrote:
>> Maxima has the function 'tex' that lets one transform an object in
>> Maxima to a string of tex code. This is quite useful: an emacs mode
>> (imaxima) uses this to create a front end that looks really
>> professional; plus, with mathjax, it can be rolled into a nice web-based
>> frontend.
>>
>> Is there anything similar in Octave?
>>
>> ----------
>> What I have in mind is the following application: I am writing course
>> notes that have a lot of numerical examples in them. With \write18, I
>> can put these inside a LaTeX file and have Octave do the computations. I
>> would like to have Octave run a hook before quitting to dump all of the
>> user-defined variables into a tex file as tex macros. It doesn't need to
>> be fancy, something like
>>
>> x=1:2; ==>
>> \def\octavex#1{\ifnum#1=1{1}\else\ifnum#1=2{2}\else\relax\fi\fi}
>>
>> IPC on the cheap, if you like. It seems like someone might have already
>> written something like this.
>>
>> Leo
>
> Look in the miscellaneous packages in the function `publish'. It
> should do what you want though I'm not sure if free of bugs. You find
> any, please fix it and send us patches.
>
> There's also a very nice textable function that hasn't been released
> yet (also from miscellaneous package). Check it out at
>
> https://sourceforge.net/p/octave/code/11184/tree/trunk/octave-forge/main/miscellaneous/inst/textable.m
>
> Carnë

Carnë, Juan, thanks for the replies.

I am not interested in type-setting octave code in latex, there are a
few ways to do that. The reference to maxima was because maxima makes it easy
to customize the tex output via texput.

Nor am I really interested in the approach taken by textable or
publish. What I want is a way to export 'stuff' from octave to tex that
preserves as much structure as possible. I want tex to do the
typesetting and octave to do the calculations.

>From your responses, I infer that there is not a readily available way
to do this. So, I scratched something together to illustrate what I
want. Here is a snippet (apologies for the apalling code):

,----
| octave> x=rand(1,3)
| x =
| 
|    0.40866   0.24822   0.95185
| 
| octave> texit(x,"x")
| ans = 
\def\x(#1){\ifnum#1<1\relax\else\ifnum#1>3\relax\else\ifnum#1=1{0.408658}\else\ifnum#1=2{0.248223}\else\ifnum#1=3{0.951853}\fi\fi\fi\fi\fi}
`----

In tex, the vector components are accessed by \x(1), etc.
To typeset \x as a vector just requires a simple tex macro to loop
over all the entries in \x. 



----------
(All code is released under gplv3; varargin is there to provide customizations):

function s = texit(object,name,varargin)
  if            isscalar(object),       s=texit_scalar(object,name,varargin);
  elseif        isvector(object),       s=texit_vector(object,name,varargin);
  elseif        ismatrix(object),       s=texit_matrix(object,name,varargin);
  endif
endfunction

function s=texit_scalar(object,name,varargin)
  s=sprintf("\\def\\%s{%f}",name,object);
endfunction

function s=texit_vector(x,name,varargin)
  n=length(x);
  c=texit_check_bounds(1,n);
  s=cstrcat("\\def\\",name,"(#1){",c);
  f="";
  for i=1:n
    t=texit_accessor(x(i),i);
    s=cstrcat(s,"\\else",t);
    f=cstrcat(f,"\\fi");
  endfor
  s=cstrcat(s,f,"\\fi\\fi}");
endfunction




reply via email to

[Prev in Thread] Current Thread [Next in Thread]