Kapitel 7

Beispiele

echnen in Tabellenspalten

\setcounter{errorcontextlines}{100}
\documentclass[ngerman,a5paper]{article}
\usepackage{babel,tabularray,luacode,siunitx}
\addto\extrasenglish{\sisetup{locale=US}}
\addto\extrasngerman{\sisetup{locale=DE}}

\begin{luacode}
  function resetSumme()
    -- initiiert allgemeines Rücksetzen beim nächsten "addiereBetrag"
    cell = {};
  end
  function addiereBetrag(r,c,s) -- Zeile, Spalte, Betrag (String!)
    if next(cell) == nil then
      print('### resetSumme ###')
      n = {};  Sum = {}
    end
    key = '' .. r .. ',' .. c
    if cell[key] == nil then -- jede Zelle nur einmal auswerten
      s = s:gsub(",",".")
      v = tonumber(s)
      cell[key] = v
      if n[c] == nil then
        Sum[c] = v
        n[c] = 1
      else
        Sum[c] = Sum[c] + v
        n[c] = n[c] + 1
      end 
      print(key .. ' **** Sum[' .. c .. ']=' .. v)
    end 
  end
  function printSumme(c,f)
    if next(cell) == nil then
      tex.print('??')
    else
      print('>>>> Sum[' .. c .. ']=' .. Sum[c] * f / 100.)
      tex.print(Sum[c] * f / 100.)
    end
  end
  function printMWert(c,f)
    if next(cell) == nil then
      tex.print('??')
    else
      print('>>>> MWert=' .. Sum[c] * f / 100. / n[c])
      tex.print(Sum[c] * f / 100. / n[c])
    end
  end
\end{luacode}

\newcommand*\resetSumme{\directlua{resetSumme()}}% ???
\AtBeginEnvironment{tblr}{\resetSumme}
\sisetup{round-mode=places,round-precision=2}

\NewTableCommand\theSum[1][100]{\SetCell[c=1]{r,cmd=\directlua{%
  printSumme(\arabic{colnum},#1)}}}
\NewTableCommand\theMWert[1][100]{\SetCell[c=1]{r,cmd=\directlua{%
  printMWert(\arabic{colnum},#1)}}}

\UseTblrLibrary{booktabs,siunitx}

\def\Summe#1{%
  %\SI{#1}\directlua{addiereBetrag(\arabic{rownum},\arabic{colnum},'#1')}%
  #1\directlua{addiereBetrag(\arabic{rownum},\arabic{colnum},'#1')}%
}

\newcommand*\RowNum[1]{\arabic{rownum}.}
\NewColumnType{A}{Q[r,cmd=\Summe]}
%\NewColumnType{A}{Q[si={},r,rmd=\Summe]}
%\NewColumnType{A}{Q[si={},r,rmd=\fbox]}
%\NewColumnType{A}{Q[si={},r]}
\NewColumnType{N}{Q[r,cmd=\RowNum]}

\begin{document}

%%%\resetSumme
\begin{tblr}[long]{colspec={N l *3A},%
  column{1}={leftsep=0pt}, column{Z}={rightsep=0pt}}
\toprule
& & 0 &  0 & 1,0012345 \\
& & 1 & -1 & 2,1  \\
& & 2 & -2 & 3,4  \\
& & 3 & -3 & 4,5  \\
& & 4 & -4 & 5,6  \\
& & 5 & -5 & 6,7  \\
\bottomrule
& Summe: & \theSum & \theSum & \theSum \\
& MwSt. (19\,\%): & \theSum[19] & \theSum[19] & \theSum[19] \\
\bottomrule
& Mittelwert: & \theMWert & \theMWert & \theMWert \\
\bottomrule
\end{tblr}

\end{document}

Zurück zur Hauptseite/Back to main page
Copyright $Id: chapter07.html 41 2023-01-16 14:53:19Z voss $