
다른 파일에 설정한 명령에 따라 일부 부분이 표시되거나 표시되지 않는 방정식을 출력하는 문서를 설정하고 있습니다.
명령은 다음과 같습니다:
\newcommand\coefficient{1.25}
이 계수가 0이 아니고 1이나 -1이 아닌 경우 방정식에 표시하고 싶습니다. 양수이면 -1.25(부호 변경)로 출력되어야 합니다. 지금까지 저는 다음과 같이 하고 있습니다.
\documentclass[border=10pt,convert={density=300,size=1080x800,outext=.png}]{standalone}
\input{coefficients.tex}
\usepackage{amsmath}
\begin{document}
$\displaystyle
y(k) =
\ifdim\dimexpr\coefficient pt =0pt
\else
\ifdim\dimexpr\coefficient pt >0pt
+
\else
\fi
\ifdim\dimexpr\coefficient pt=1pt
\else
\ifdim\dimexpr\coefficient pt=-1pt
-
\else
\coefficient
\fi
\fi
x(k-1)
\fi
+ 5
$
\end{document}
이것이 수행하는 작업은 값이 0, 1 또는 -1이 아닌 경우 값을 표시하고, -1인 경우 -를 표시하고, 값이 0인 경우 x(k-1) 부분을 표시하지 않는 것입니다.
이제 내 방정식의 경우 명령에 정의된 모든 양수 값이 방정식에서 음수로 출력되고 모든 음수 값이 양수로 출력되도록(더 어렵습니다) 부호를 변경하고 싶습니다.
어떻게 해야 합니까(추가 패키지를 사용하지 않는 것이 좋습니다)?
답변1
원하는 것을 수행하는 솔루션은 다음과 같습니다. 불행하게도 패키지를 로드하지 않고 정상적인 입력 방식으로 부동 소수점 연산을 수행하는 것은 불가능하므로 이 명령은 연산을 수행할 수 없습니다.
\documentclass{article}
\makeatletter
\def\printcoefficient#1#2{%
\ifdim\dimexpr#1pt=0pt\else
\ifdim\dimexpr#1 pt >0pt%
+%
\fi
\ifdim\dimexpr#1pt=1pt\else
\ifdim\dimexpr#1pt=-1pt%
-%
\else
\expandafter\pc@removepoint\the\dimexpr#1pt%
\fi
\fi
#2%
\fi
}
\def\printnegativecoefficient#1{\printcoefficient{-#1}}
\begingroup\lccode`8=`p\lccode`9=`t\lowercase{\endgroup
\def\pc@removepoint#189{#1}
}
\makeatother
\begin{document}
$1\printcoefficient{0}{x}$
$1\printcoefficient{1}{x}$
$1\printcoefficient{2}{x}$
$1\printcoefficient{-1}{x}$
$1\printcoefficient{-1.2}{x}$
$1\printcoefficient{1.2}{x}$
$1\printnegativecoefficient{0}{x}$
$1\printnegativecoefficient{1}{x}$
$1\printnegativecoefficient{2}{x}$
$1\printnegativecoefficient{-1}{x}$
$1\printnegativecoefficient{-1.2}{x}$
$1\printnegativecoefficient{1.2}{x}$
\end{document}
답변2
너~할 수 있었다추가 패키지 없이도 할 수 있는데 왜 그럴까요?
\documentclass[border=10pt]{standalone}
\usepackage{amsmath}
\usepackage{xfp} % also loads expl3
\ExplSyntaxOn
\NewExpandableDocumentCommand{\cf}{sm}
{
\fp_compare:nF { #2 < \c_zero_fp } { \IfBooleanF { #1 } { + } }
\fp_compare:nF { #2 = \c_one_fp }
{
\fp_compare:nTF { #2 = -\c_one_fp }
{ - }
{ #2 }
}
}
\ExplSyntaxOff
\newcommand{\constA}{-2.5}
\newcommand{\constB}{1.35}
\newcommand{\constC}{1}
\newcommand{\constD}{-1}
\begin{document}
$\displaystyle
y(k) =
\cf{\constA}x(k-1)
\cf{\constB}x^2
\cf{\constC}x^3
\cf{\constD}x^4
\qquad
z(k) =
\cf*{\constC}{x^3}
$
\end{document}
-version *
은 인쇄할 첫 번째 계수에 대한 것입니다. 양수인 경우 부호 +
를 생략해야 합니다.