···<date: 2012-10-25, Thursday>···<from: Otared Kavian>···

> Hi Philipp,
> 
> Thanks to you, Dalyoung and Hans for the nice example using luacode.
> I was wonedring how would you modify your code in order to be able to change 
> the name of the matrix if necessary. For instance, how to modify your code in 
> such a way that upon saying
>        \ctxlua{document.print_matrix(b,3)}
> one gets a 3x3 matrix whos coefficients are b_{11},…b_{33}.

This may be overkill but you can just wrap the matrix command in
some TeX macro:

·································································
% macros=mkvi
\unprotect
\startluacode
  local context = context
  commands.matrix_with_symbol = function (sym, x, y)
    if not y then y = x end -- default to square
    context.startmatrix{ left = "\\left(\\,", right = "\\,\\right)" }
    local schema = sym .. "_{%d%d}" -- %d: prints integer part
    for i=1, x do
      for j=1, y do
        context.NC()
        context(schema, i, j)
      end
      context.NR()
    end
    context.stopmatrix()
  end
\stopluacode

\unexpanded\def\printsymbolmatrix{%
  \dotripleempty\print_symbol_matrix_indeed%
}

\def\print_symbol_matrix_indeed[#first][#second][#num]{%
  \bgroup
  \def\sym{a}%% set default symbol to print, could be done in Lua
  \def\y{nil}%
  \ifsecondargument
    \ifthirdargument
      \edef\sym{#first}%
      \edef\x{#second}%
      \edef\y{#num}%
    \else
      \doifnumberelse{#first}%
        {\edef  \x{#first}\edef\y{#second}}%
        {\edef\sym{#first}\edef\x{#second}}%
    \fi
  \else
    \edef\x{#first}%
  \fi
  \ctxcommand{matrix_with_symbol(\!!bs\sym\!!es, \x, \y)}%
  \egroup%
}
\protect

\starttext
  \startformula
    \printsymbolmatrix[3]%% symbol is “a”, matrix is square
  \stopformula
  \startformula
    \printsymbolmatrix[4][8]%% symbol is “a”, matrix is 4x8
  \stopformula
  \startformula
    \printsymbolmatrix[b][8]%% symbol is “b”, matrix is square
  \stopformula
  \startformula
    \printsymbolmatrix[c][5][3]%% symbol is “c”, matrix is 5x3
  \stopformula
\stoptext
\endinput
·································································

Now the symbol will default to “a” but you can specify another
one. (Btw. I think the Lua part could be done as easily with
dorecurse.)

Regards
Philipp



> 
> Thanks in advance: OK
> 
> On 25 oct. 2012, at 03:02, Philipp Gesang 
> <philipp.ges...@alumni.uni-heidelberg.de> wrote:
> 
> > ···<date: 2012-10-25, Thursday>···<from: Jeong Dal>···
> > 
> >> Dear Hans, Lucas, Wolfgang, Aditya
> >> 
> >> Now, I write a matrix using \startluacode by the helps of  you.
> >> Also, I can do some operations in matrices which reduced my typing job.
> >> In this code, I have to give all the entries of a matrix as a table. It is 
> >> good to use in many cases.
> >> 
> >> I have one more question. If the given array is as the following and it 
> >> works well.
> >> 
> >> However, there might be a better way to do job using "for" iteration.
> >> I tired to use "i, j" in several ways but it doesn't work.  
> > 
> > Inside a string the variables are just bytes. To print to a
> > string you can interpolate with string.format() [1].
> > 
> > [1] http://www.lua.org/manual/5.1/manual.html#pdf-string.format
> > 
> > ·································································
> > \startluacode
> >  document = document or { } -- recommended: use namespace
> >  document.print_matrix = function (x, y)
> >    if not y then y = x end -- default to square
> >    context.startmatrix{left = "\\left(\\,", right = "\\,\\right)"}
> >    local schema = "a_{%d%d}" -- %d: prints integer part
> >    for i=1, x do
> >      for j=1, y do
> >        --- use the template defined above to print the
> >        --- row and column
> >        context.NC(string.format(schema, i, j))
> >      end
> >      context.NR()
> >    end
> >    context.stopmatrix()
> >  end
> > \stopluacode
> > 
> > \starttext
> >  \startformula
> >    \ctxlua{document.print_matrix(3)}
> >  \stopformula
> >  \startformula
> >    \ctxlua{document.print_matrix(9,4)}
> >  \stopformula
> >  \startformula
> >    \ctxlua{document.print_matrix(9)}
> >  \stopformula
> > \stoptext
> > \endinput
> > ·································································
> > 
> >> 
> >> Is there a way to do such a job using "for" iteration?
> >> 
> >> Thank you for reading.
> >> 
> >> Best regards,
> >> 
> >> Dalyoung
> >> ___________________________________________________________________________________
> >> If your question is of interest to others as well, please add an entry to 
> >> the Wiki!
> >> 
> >> maillist : ntg-context@ntg.nl / 
> >> http://www.ntg.nl/mailman/listinfo/ntg-context
> >> webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
> >> archive  : http://foundry.supelec.fr/projects/contextrev/
> >> wiki     : http://contextgarden.net
> >> ___________________________________________________________________________________
> > 
> > -- 
> > ()  ascii ribbon campaign - against html e-mail
> > /\  www.asciiribbon.org   - against proprietary attachments
> > ___________________________________________________________________________________
> > If your question is of interest to others as well, please add an entry to 
> > the Wiki!
> > 
> > maillist : ntg-context@ntg.nl / 
> > http://www.ntg.nl/mailman/listinfo/ntg-context
> > webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
> > archive  : http://foundry.supelec.fr/projects/contextrev/
> > wiki     : http://contextgarden.net
> > ___________________________________________________________________________________
> 
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the 
> Wiki!
> 
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
> archive  : http://foundry.supelec.fr/projects/contextrev/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________

-- 
()  ascii ribbon campaign - against html e-mail
/\  www.asciiribbon.org   - against proprietary attachments

Attachment: pgpQG3leK36bu.pgp
Description: PGP signature

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

Reply via email to