테이블 형식 환경의 셀에 합계를 구성합니다. 간단한 산술을 위해 매크로 확장이나 인라인 평가가 가능합니까?

테이블 형식 환경의 셀에 합계를 구성합니다. 간단한 산술을 위해 매크로 확장이나 인라인 평가가 가능합니까?

저는 12개 정도의 항목별 라인이 있고 각 라인에는 숫자 정수 비용이 포함된 테이블 형식 환경이 있습니다. 마지막 행에 합계를 표시하고 싶습니다. 나는 일정 기간 동안 테이블의 항목을 여러 번 편집했는데, 매번 총계를 지루하게 다시 계산했습니다.

간단한 산술 공식에 대해 인라인 평가를 수행할 수 있는 방법이 있습니까? 예를 들어 bash의 $(())와 유사한 것입니다. The total is $((12 + 34 + 23 + ... + 5))여기서 리터럴 방정식이 아닌 평가된 표현식이 표시됩니다. 항목의 행 값을 변경하고 방정식에서 해당 숫자도 변경하면 좋을 것입니다(여기서는 전체 스프레드시트 표현이 필요하지 않다고 생각합니다).

또한 템플릿에서 테이블을 생성하기 위한 전처리 단계를 추가하지 않는 것이 좋습니다. 테이블의 크기는 아직 그것을 보증하지 않습니다.

소스 문서와 함께 인라인으로 정의된 계산 결과를 표시하려면 tex에 간단한 내장 방법이 있어야 한다고 생각합니다. 사용자 정의 카운터를 남용하거나, 새로운 명명된 길이를 정의하고, 여기에 단위를 추가한 다음 해당 값을 표시함으로써?

다음과 같은 것을 달성하려고 합니다(의사 코드).

% initialize the current total to 0 before the table
{\thetotal = 0}

\begin{tabular}{@{}lp{4cm}r@{}} \toprule
  Store & Purpose & Items\\
  \midrule
  Safeway & for the salad   &    10 tomatoes  {\thetotal += 10} \\
  Whole Foods & for dessert &     4 muffins   {\thetotal += 4}  \\
  % ...
  % many more rows here.
  % in each row, \thetotal is incremented. This way, if I change
  % one line's value, I can change just that line and I don't need
  % to change anything else.
  % ...
  Home Depot & leftovers &     17 mouse traps {\thetotal += 17} \\
  \bottomrule
  % Lastly, show the value of the counter at that point in the document
  \multicolumn{2}{r}{Total} & \thetotal items
\end{tabular}

Total 31마지막 행의 마지막 열에서 문서의 해당 지점(예: 이 예) 의 누적 집계를 표시하고 싶습니다 .

답변1

정수 값만 포함되므로 \addtocounter명령만으로 충분합니다.

테이블을 약간 변경하고 \additems자동으로 계산을 수행하는 명령을 추가했습니다. 최종 테이블 디자인은 OP에 맡깁니다.

\documentclass{article}

\usepackage{booktabs}
\usepackage{siunitx}
\newcounter{total}
\newcommand{\additem}[2]{%
  \num{#1} & #2 &   \addtocounter{total}{#1} \thetotal
}

\begin{document}

\begin{tabular}{@{}lp{4cm}rlr@{}} \toprule
  Store & Purpose & \multicolumn{3}{c}{Items} \tabularnewline
  \midrule
  Safeway & for the salad   &    \additem{10}{tomatoes} \tabularnewline
  Whole Foods & for dessert &    \additem{4}{muffins}   \tabularnewline
  Home Depot & leftovers &     \additem{17}{mouse traps} \tabularnewline
  \bottomrule
  \multicolumn{4}{r}{Total} & \thetotal\ items
\end{tabular}


\end{document}

여기에 이미지 설명을 입력하세요

합계가 없는 다른 버전

\documentclass{article}

\usepackage{booktabs}
\usepackage{siunitx}
\newcounter{total}
\newcommand{\additem}[2]{%
  \num{#1} & #2    \addtocounter{total}{#1} 
}

\begin{document}

\begin{tabular}{@{}lp{4cm}rl@{}} \toprule
  Store & Purpose & \multicolumn{2}{c}{Items} \tabularnewline
  \midrule
  Safeway & for the salad   &    \additem{10}{tomatoes} \tabularnewline
  Whole Foods & for dessert &    \additem{4}{muffins}   \tabularnewline
  Home Depot & leftovers &     \additem{17}{mouse traps} \tabularnewline
  \bottomrule
  \multicolumn{2}{r}{Total} & \thetotal & %items
\end{tabular}


\end{document}

여기에 이미지 설명을 입력하세요

관련 정보