gpt4 book ai didi

latex - 如何在 LaTeX/TikZ 中执行 "complex"计算

转载 作者:行者123 更新时间:2023-12-04 20:29:58 26 4
gpt4 key购买 nike

我希望这个问题已经被回答了一千次,但我找不到解决我的问题的方法 =(

我想计算 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

所以基本上我的问题是: 在 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

不幸的是,我无法将\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)}

使用时可以加回单位: \draw (0,0) -- ({\heightpernodeA ex},{1});

关于latex - 如何在 LaTeX/TikZ 中执行 "complex"计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48508232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com