我希望这个问题已经被回答了上千次,但是我只是无法找到解决问题的方法=(
我想在LaTeX(TikZ)中计算一些值,其中一些是长度(例如10 pt),有些则不是。只要计算形式非常简单,就像a*b+c
一切都很好,但是如果我需要像(a+b)*(c+d)
LaTeX 这样的括号,就会抱怨。此外,如果我有嵌套定义,则无法按预期解决这些问题。例:
\def\varA{1+2}
\def\varB{10}
% I want to calculate (1+2)*10 or 10*(1+2), respectively.
\def\varC{\varA*\varB} % evaluates to 21
\def\varD{\varB*\varA} % evaluates to 12
Run Code Online (Sandbox Code Playgroud)
所以基本上我的问题是:在LaTeX中进行计算的正确(或推荐)方法是什么?
作为一个更现实的示例,这是我实际上想要做的,但不能做的:
% total height of my TikZ image
\def\myheight{20ex}
% five nodes shall be drawn as a stack
\def\numnodes{5}
% but there shall be some space at the top (like a 6th node)
\def\heightpernodeA{\myheight / (\numnodes + 1)} % fails whenever I want to use it
% workaround?
\def\numnodesplusone{\numnodes + 1}
\def\heightpernodeB{\myheight / \numnodesplusone} % fails for the reason explained above
Run Code Online (Sandbox Code Playgroud)
不幸的是,由于将变量用于各种计算,因此我无法将\ numnodes重新定义为6。
最好的祝福
您可以\pgfmathsetmacro
用于更复杂的计算。不过,您需要移除该单元myheight
:
% total height of my TikZ image
\def\myheight{20}
% five nodes shall be drawn as a stack
\def\numnodes{5}
% but there shall be some space at the top (like a 6th node)
\pgfmathsetmacro\heightpernodeA{\myheight / (\numnodes + 1)}
Run Code Online (Sandbox Code Playgroud)
您可以在使用时重新添加该单元: \draw (0,0) -- ({\heightpernodeA ex},{1});
归档时间: |
|
查看次数: |
3301 次 |
最近记录: |