ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Philipp Gesang <Philipp.Gesang@alumni.uni-heidelberg.de>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: using variable for subscript in math+lua
Date: Fri, 26 Oct 2012 00:05:13 +0200	[thread overview]
Message-ID: <20121025220513.GA24819@phlegethon.router_intern> (raw)
In-Reply-To: <C4EB3BDF-3460-4E20-9E18-330873C34D25@gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 6651 bytes --]

···<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.Gesang@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 #1.2: Type: application/pgp-signature, Size: 198 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

  parent reply	other threads:[~2012-10-25 22:05 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-25  0:38 Jeong Dal
2012-10-25  1:02 ` Philipp Gesang
2012-10-25  7:52   ` Hans Hagen
2012-10-25  8:01     ` Philipp Gesang
2012-10-25  8:28       ` Hans Hagen
2012-10-25 18:02   ` Otared Kavian
2012-10-25 20:11     ` Hans Hagen
2012-10-25 21:27     ` Procházka Lukáš
2012-10-25 22:05     ` Philipp Gesang [this message]
2012-10-26  6:29 Jeong Dal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20121025220513.GA24819@phlegethon.router_intern \
    --to=philipp.gesang@alumni.uni-heidelberg.de \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).