4

The next code gives 5e-05 with \luaveclen{0.00003}{0.00004} but I need a decimal How to get a decimal notation ?

\def\luaveclen#1#2{
    \directlua{
        x = #1;
        y = #2;
       r=(x*x+y*y)^0.5
       tex.print(tostring(r))}
}

with \luaveclen{0.0003}{0.0004} the result is 0.0005.

I need a decimal because here problem with veclen my answer now is wrong.

I find a solution with fpu but it's not elegant:

%!TEX TS-program =  lualatex
\documentclass[11pt]{article}
\usepackage{fontspec}
\usepackage{luatextra}   
\usepackage{tikz}
\usetikzlibrary{decorations.markings,fpu}   

\begin{document}

\makeatletter     

\def\luaveclen#1#2{
    \directlua{
        x = #1;
        y = #2;
       r=(x*x+y*y)^0.5
       tex.print(tostring(r))}
}

\pgfmathdeclarefunction*{veclen}{2}{%
\begingroup
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}
\pgfmathparse{\luaveclen{#1}{#2}}
  \edef\pgfmath@tmp{\pgfmathresult}
  \pgfmath@returnone\pgfmath@tmp pt
\endgroup
}
\makeatother  

\pgfmathparse{veclen(0.00003,0.00004)}
 Vector length is: \pgfmathresult    

\begin{tikzpicture}[decoration={markings, mark = at position .5 with
 {\draw (-2pt,-2pt) -- (2pt,2pt)  (2pt,-2pt) -- (-2pt,2pt);}}]    

\draw [postaction={decorate}] (0,0) -- ++(146:1) arc (146:157:1) -- (0,0);
\end{tikzpicture} 
\luaveclen{0.0003}{0.0004}

\end{document} 

1 Answer 1

5

You can use the string.format function:

%!TEX TS-program =  lualatex
\documentclass[11pt]{article}
\usepackage{fontspec}
\usepackage{luatextra}   
\usepackage{tikz}
\usetikzlibrary{decorations.markings,fpu}   
\usepackage{luacode}

\begin{document}

\makeatletter     
\begin{luacode*}
function get_real_number(r)
return string.format("%f", r)
end
\end{luacode*}
\def\luaveclen#1#2{%
  \directlua{
x = #1;
y = #2;
r=(x*x+y*y)^0.5
tex.print(get_real_number(r))
}}

\luaveclen{0.0003}{0.0004}

\end{document} 
4
  • Thanks ! Is it possible to avoid creating a new function like "get_real_number" ? Nov 24, 2019 at 18:04
  • @AlainMatthes there is a catcode problem because of the % character is interpreted as a TeX comment, so this is the easiest solution. I am sure it would be possible to enable percent char also using catcodes
    – michal.h21
    Nov 24, 2019 at 19:25
  • 1
    @AlainMatthes Use tex.print(string.format("\csstring\%f", r)) to avoid having to define the additional get_real_number function. Nov 25, 2019 at 2:53
  • @HenriMenke Perfect ! It's difficult to find this in the manual !! Nov 25, 2019 at 10:12

You must log in to answer this question.

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