带有输入值和答案的条件公式格式

带有输入值和答案的条件公式格式

使用下面的 luacode,我尝试生成一个条件公式格式,其中包含输入的值(包括答案​​)。但是,这似乎不必要地复杂,并且公式格式不起作用。底部是所需的结果,但需要手动操作。如何才能更好、更有效地做到这一点?

公式

\documentclass[paper=A4, 11pt]{scrreprt}
\usepackage{luacode}
\usepackage{amsmath}
\begin{document}
\begin{luacode*}
function beta (a, L)
    local o
    local beta = a / L
    o = string.format("%.2f", beta)
    tex.print(o)
end
function formula_variable (xi, a, L)
    local o
    local beta = a / L
    local left = "-1/2*beta^2*xi + beta*xi - 1/2*xi + 1/6*beta^3 - 1/2*beta + 1/3"
    local right = "1/6*xi^3 - 1/2*beta*xi^2 + beta*xi - 1/2*xi - 1/2*beta + 1/3"
    if (xi >= 0 and xi < beta) then
        o = string.format(left)
    else
        o = string.format(right)
    end
    tex.sprint(string.format(o,xi,beta))
end
function formula_entered (xi, a, L)
    local o
    local beta = a / L
    local left = "-1/2*beta^2*xi + beta*xi - 1/2*xi + 1/6*beta^3 - 1/2*beta + 1/3"
    local right = "1/6*xi^3 - 1/2*beta*xi^2 + beta*xi - 1/2*xi - 1/2*beta + 1/3"
    if (xi >= 0 and xi < beta) then
        o = left
    else
        o = right
    end
    tex.sprint(string.format(o,xi,beta))
end
function formula_answer (xi, a, L)
    local o
    local beta = a / L
    local left = -1/2*beta^2*xi + beta*xi - 1/2*xi + 1/6*beta^3 - 1/2*beta + 1/3
    local right = 1/6*xi^3 - 1/2*beta*xi^2 + beta*xi - 1/2*xi - 1/2*beta + 1/3
    if (xi >= 0 and xi < beta) then
        o = string.format("%.4f", left)
    else
        o = string.format("%.4f", right)
    end
    tex.print(o)
end
\end{luacode*}

\def\XI{0.5}
\def\L{5.0}     % length
\def\a{3.5}     % distance
\def\BETA{\directlua{beta(\a,\L)}}

\newcommand{\FORMULA}[1]{\directlua{formula_variable(#1)}}
\newcommand{\ENTERED}[1]{\directlua{formula_entered(#1)}}
\newcommand{\ANSWER}[1]{\directlua{formula_answer(#1)}}

Automatic (but not as desired)
\begin{align*}
  y_{max}   &= \FORMULA{\XI,\a,\L}\\
            &= \ENTERED{\XI,\a,\L}\\
            &= \ANSWER{\XI,\a,\L}
\end{align*}

Manual (with formatting as desired)
\begin{align*}
  y_{max}   &= -\frac{1}{2}\cdot\beta^2\cdot\xi
                + \beta\cdot\xi
                - \frac{1}{2}\cdot\xi
                + \frac{1}{6}\cdot\beta^3
                - \frac{1}{2}\cdot\beta
                + \frac{1}{3}\\
            &= -\frac{1}{2}\cdot\BETA^2\cdot\XI
                + \BETA\cdot\XI
                - \frac{1}{2}\cdot\XI
                + \frac{1}{6}\cdot\BETA^3
                - \frac{1}{2}\cdot\BETA
                + \frac{1}{3}\\
            &= \ANSWER{\XI,\a,\L}
\end{align*}

\end{document}

答案1

像这样吗?

在此处输入图片描述

\documentclass[paper=A4, 11pt]{scrreprt}
\usepackage{luacode}
\usepackage{amsmath}
\begin{document}

\begin{luacode*}
function beta ( a , L )
    tex.sprint ( string.format ( "%.2f" , a/L ) )
end

function formula_variable ( xi , a , L )
    local beta, zz
    beta  = a / L
    if ( xi >= 0 and xi < beta ) then
       zz = "-1/2*\\beta^2*\\xi + \\beta*\\xi - 1/2*\\xi + 1/6*\\beta^3 - 1/2*\\beta + 1/3"
    else
       zz = "1/6*\\xi^3 - 1/2*\\beta*\\xi^2 + \\beta*\\xi - 1/2*\\xi - 1/2*\\beta + 1/3"
    end
    tex.sprint ( zz )
end

function formula_entered (xi, a, L)
    local beta, zz
    beta  = a / L
    if ( xi >= 0 and xi < beta ) then
       zz = ""..(-1/2).."*"..(beta^2).."*" ..xi.. "+" ..beta.. "*" ..xi..(-1/2).."*"..xi.. "+"..string.format("%.4f",1/6).."*"..(beta^3)..(-1/2).."*"..beta.."+"..string.format("%.4f",1/3)
    else
       zz = "1/6*"..(xi^3).."-1/2*"..beta.."*"..(xi^2).."+"..beta.."*"..xi.."-1/2*"..xi.."-1/2*"..beta.."+1/3"
    end
    tex.sprint ( zz )
end
    
function formula_answer (xi, a, L)
    local beta, zz
    beta  = a / L
    if ( xi >= 0 and xi < beta ) then
       zz = -(1/2)*beta^2*xi + beta*xi - (1/2)*xi + (1/6)*beta^3 - (1/2)*beta + (1/3)
    else
       zz = (1/6)*xi^3 - (1/2)*beta*xi^2 + beta*xi - (1/2)*xi - (1/2)*beta + (1/3)
    end
    tex.sprint ( string.format ( "%.4f" , zz ) )
end
\end{luacode*}

\def\XI{0.5}
\def\L{5.0}     % length
\def\a{3.5}     % distance
\def\BETA{\directlua{beta(\a,\L)}}

\newcommand{\FORMULA}[1]{\directlua{formula_variable(#1)}}
\newcommand{\ENTERED}[1]{\directlua{formula_entered(#1)}}
\newcommand{\ANSWER}[1]{\directlua{formula_answer(#1)}}

Automatic
\begin{align*}
  y_{\max}   
     &= \FORMULA{\XI,\a,\L} \\
     &= \ENTERED{\XI,\a,\L} \\
     &= \ANSWER{\XI,\a,\L}
\end{align*}

Manual
\begin{align*}
  y_{\max}   
     &= -\tfrac{1}{2}\cdot\beta^2\cdot\xi
        + \beta\cdot\xi
        - \tfrac{1}{2}\cdot\xi
        + \tfrac{1}{6}\cdot\beta^3
        - \tfrac{1}{2}\cdot\beta
        + \tfrac{1}{3} \\
     &= -\tfrac{1}{2}\cdot\BETA^2\cdot\XI
        + \BETA\cdot\XI
        - \tfrac{1}{2}\cdot\XI
        + \tfrac{1}{6}\cdot\BETA^3
        - \tfrac{1}{2}\cdot\BETA
        + \tfrac{1}{3} \\
     &= \ANSWER{\XI,\a,\L}
\end{align*}

\end{document}

相关内容