
我嘗試為高中物理教科書中使用的通用來源創建一個電路符號:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{circuits.ee.IEC,shapes.gates.ee}
\makeatletter
\pgfdeclareshape{mybox}{%
\inheritsavedanchors[from=rectangle ee]
\inheritanchor[from=rectangle ee]{center}
\inheritanchor[from=rectangle ee]{north}
\inheritanchor[from=rectangle ee]{south}
\inheritanchor[from=rectangle ee]{east}
\inheritanchor[from=rectangle ee]{west}
\inheritanchor[from=rectangle ee]{north east}
\inheritanchor[from=rectangle ee]{north west}
\inheritanchor[from=rectangle ee]{south east}
\inheritanchor[from=rectangle ee]{south west}
\inheritanchor[from=rectangle ee]{input}
\inheritanchor[from=rectangle ee]{output}
\inheritanchorborder[from=rectangle ee]
\inheritbackgroundpath[from=rectangle ee]
\anchor{west circ center}{
\pgfpointlineattime{0.5}{\southwest}{\northeast}
\advance\pgf@x by-0.5\tikzcircuitssizeunit%
}
\anchor{east circ center}{
\pgfpointlineattime{0.5}{\southwest}{\northeast}
\advance\pgf@x by0.5\tikzcircuitssizeunit%
}
\backgroundpath{%
\pgfpathmoveto{\pgfpointanchor{\tikz@fig@name}{west}}
\pgfpathlineto{\pgfpointanchor{\tikz@fig@name}{west circ center}}
\pgfpathmoveto{\pgfpointanchor{\tikz@fig@name}{east}}
\pgfpathlineto{\pgfpointanchor{\tikz@fig@name}{east circ center}}
\pgfusepath{stroke}
\pgfpathcircle{\pgfpointanchor{\tikz@fig@name}{east circ center}}{0.25\tikzcircuitssizeunit}
\pgfpathcircle{\pgfpointanchor{\tikz@fig@name}{west circ center}}{0.25\tikzcircuitssizeunit}
\pgfusepath{stroke,fill}
}
}
\makeatother
\tikzset{
circuit declare symbol=general source,
set general source graphic={
draw,fill=white,shape=mybox, circuit symbol size=width 3 height 1,
transform shape,
}
}
\begin{document}
\begin{tikzpicture}[circuit ee IEC]
\draw(0,0)to[general source={info={foo}}](2,0)to[resistor={info=bar}](4,0);
\draw(0,0)to[resistor](0,-3)to[make contact](4,-3)to[bulb](4,0);
\end{tikzpicture}
\end{document}
是使用和\backgroundpath
等命令繪製的,該命令在我的機器上運行。一旦建立了其他錨點,它本身就會使用背景路徑和僅基於 的硬計算來定義自己的派生形狀,這對我來說看起來很麻煩。\pgfpathlineto
\pgfpointanchor
tikz
\southwest
\northeas
我的方式有什麼傷害或風險嗎tikz
?
答案1
簡而言之,你沒有錯。這取決於你是否值得。
PGF 手冊明確指出\anchor
,與 相比\savedanchor
,按需創建目的。也就是說,只有當使用者明確寫入 時,才會計算錨點的座標(A.east)
。
這節省了大量時間,因為 90% 的錨點根本沒有被使用,我們不應該浪費任何時間來計算它們。
當鈦kZ正在繪製背景路徑,這些\anchor
座標尚未計算。如果鈦kZ 計算這些座標只是為了繪圖,那麼\anchor
首先使用它是沒有意義的......只需製作所有內容\savedanchor
。
所以這與 TeX 相容性無關。鈦kZ 喜歡純 TeX 程式碼,因為它們更快。作者決定再花兩個小時,為數百萬人節省兩秒鐘。在我看來,我們應該說 TikZ 是為 TeX 設計的,並且與 LaTeX 相容。
就您而言,一切都取決於您。您想編寫簡單的程式碼並等待幾毫秒嗎?還是你更喜歡玩\pgf@x
?我個人比較喜歡前者。