9

The macro runs except when the real b is negative. Why ?

\documentclass[a4paper,12pt]{article}
\usepackage[upright]{fourier}
\usepackage[no-math]{fontspec}
\usepackage{luacode,luatextra,luaotfload}
\begin{document}
\newcommand\racine[3]{%
\luaexec{
local d=math.pow(#2,2)-4*#1*#3
local r_1
local r_2
local r
if d>0 then
r_1=(-#2-math.sqrt(d))/2*#1
r_2=(-#2+math.sqrt(d))/2*#1
tex.print("The discriminant is\\ " .. d .. ".\\par")
tex.print("The first root is\\ " .. r_1 .. ".\\par")
tex.print("The second root is\\ " .. r_2 .. ".\\par")
elseif d==0 then
r=-#2/2*#1
tex.print("The discriminant is\\ " .. d .. ".\\par")
tex.print("The double root is\\ " .. r .. ".\\par")
elseif d < 0 then
tex.print("The discriminant is\\ " .. d .. ".\\par")
tex.print("There are no real solutions.") 
end
}}

\racine{10}{-3}{5}
\end{document}

The error message is :

! LuaTeX error [string "\directlua "]:1: unexpected symbol near <eof>.
\luacode@dbg@exec ...code@maybe@printdbg {#1} #1 }

l.28 \racine{10}{-3}{5}

?

I would like the solutions, when possible, be written as a fraction.

2 Answers 2

13

You can not use --3 as a lua 3, you need -(-3) so:

\documentclass[a4paper,12pt]{article}
\usepackage[upright]{fourier}
\usepackage[no-math]{fontspec}
\usepackage{luacode,luatextra,luaotfload}
\begin{document}
\newcommand\racine[3]{%
\luaexec{
local d=math.pow(#2,2)-4*#1*#3
local r_1
local r_2
local r
if d>0 then
r_1=(-(#2)-math.sqrt(d))/2*(#1)
r_2=(-(#2)+math.sqrt(d))/2*(#1)
tex.print("The discriminant is\\ " .. d .. ".\\par")
tex.print("The first root is\\ " .. r_1 .. ".\\par")
tex.print("The second root is\\ " .. r_2 .. ".\\par")
elseif d==0 then
r=-(#2)/2*(#1)
tex.print("The discriminant is\\ " .. d .. ".\\par")
tex.print("The double root is\\ " .. r .. ".\\par")
elseif d < 0 then
tex.print("The discriminant is\\ " .. d .. ".\\par")
tex.print("There are no real solutions.") 
end
}}

\racine{10}{-3}{5}
\end{document}
1
  • @DavidThank you for the speed and relevance of your answers.
    – Fabrice
    Aug 28, 2013 at 22:39
12

David has already answered why you get an error message. My answer is more of an extended comment on that. The reason for the error (#1 etc begin treated literally inside the lua code) is one of the reason that the preferred style in ConTeXt is to separate the Lua code from the TeX definitions. For example, in ConTeXt, I would code the above as

\startluacode
  function commands.racine(a,b,c)
    local d= b*b - 4*a*c
    context("The discriminant is %f \\crlf", d )
    if d>0 then
        local r_1=(-b-math.sqrt(d))/(2*a)
        local r_2=(-b+math.sqrt(d))/(2*a)
        context("The first root is %f \\crlf "  , r_1 )
        context("The second root is %f \\crlf" , r_2 )
    elseif d==0 then
        local r = -b/(2*a)
        context("The double root is %f \\crlf", r)
    else
      context("There are no real solutions.\\crlf") 
    end
    context("\\par")
  end
\stopluacode

\define[3]\racine{\ctxcommand{racine(#1, #2, #3)}}

\starttext
\racine{10}{-3}{5}
\stoptext

which works as expected.

FWIU, LaTeX does not have a real equivalent to the ConTeXt luacode environment. The LaTeX luacode environments are not as robust and the preferred solution is not to use them.

5
  • Do I take it that in ConTeXt there is no assumption that \start... ... \stop... should form a group?
    – Joseph Wright
    Aug 28, 2013 at 18:46
  • @Joseph, that is right. In ConTeXt most start stop environments that are supposed to be used in the preamble (starttexdefinition, startsetups, starttypescript, etc) do not form a group. Neither does startluacode.
    – Aditya
    Aug 28, 2013 at 19:24
  • @Aditya Can you explain this : " ... %f \\crlf " , r_1 ?
    – Fabrice
    Aug 28, 2013 at 23:04
  • 1
    @Fabrice: context(" ... %f ..", r_1) is equivalent to tex.print(string.format("... %f ....", r_1)). In ConTeXt \crlf is same as `\` in LaTeX.
    – Aditya
    Aug 28, 2013 at 23:14
  • If I test the program, it usually works. But if I test it with \racine{-6}{108}{-270}, it returns me like 540 and 108 solutions instead of 3 and 15.
    – Fabrice
    Mar 17, 2014 at 19:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .