숫자변수 정의

숫자변수 정의

tex 문서에서 일부 변수를 정의하고 싶습니다. 예를 들어 .tex다음과 같이 작성합니다 Our tool succeeds to validate x samples by Method A, and y samples by Method B. Thus its score is z. Where z는 으로 정의되며 x+y( xresp. y)는 숫자로 다른 곳에서 인스턴스화됩니다(예: 30(resp. 50)). 결과적으로 컴파일 후 텍스트는 Our tool succeeds to validate 30 samples by Method A, and 50 samples by Method B. Thus its score is 80.

이것의 장점은 텍스트의 모든 숫자를 변경하려면 일부 변수의 값만 변경하면 된다는 것입니다.

누구든지 이것을 달성하는 방법을 알고 있습니까?

답변1

\documentclass{article}

\newcommand\x{30}
\newcommand\y{50}
\begin{document}

 I would like to define some variables in a tex document. For example,
 I write in .tex something similar as: Our tool succeeds to validate \x\
 samples by Method A, and \y\ samples by Method B. Thus its score is
 \the\numexpr\x+\y\relax. 

\end{document}

산출-

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

답변2

expl3기능, 특히 xfp패키지를 \fpeval사용할 수 있습니다 \inteval.

\documentclass{article}
\usepackage{xfp}

\newcommand\x{30}
\newcommand\y{50}

\begin{document}

I would like to define some variables in a \TeX{} document. For example,
I write in \texttt{.tex} something similar as: Our tool succeeds to
validate \inteval{\x} samples by Method~A, and \inteval{\y} samples by
Method~B. Thus its score is \inteval{\x+\y}.

\end{document}

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

물론 \x{}대신 사용할 수도 있지만 \inteval{\x}이 방법을 사용하면 \inteval다음을 수행할 수도 있습니다.

\newcommand{\z}{\x+3*\y}

을 사용하고 \inteval{\z}.

이는 정수에 대한 연산을 가정합니다. 부동 소수점이 필요한 경우 \fpeval대신 사용할 수 있습니다 \inteval(그러나 상황이 더 복잡해집니다).

답변3

이를 위해 카운터를 사용할 수 있습니다.

\newcounter{x}
\setcounter{x}{30}
\newcounter{y}
\setcounter{y}{50}
Our tool succeeds to validate \arabic{x} samples by Method A,
and \arabic{y} samples by Method B. Thus its score is 
\the\numexpr\value{x}+\value{y}.

관련 정보