问题
我正在.pic
使用 定义几个键,tikzset
以便稍后在其他库中使用。但是,当我使用 时pdflatex
,我收到容量超出错误。
我刚刚测试了一下,目前看来使用lualatex
似乎没问题。但是,我不想只限制在一个程序上编译文档。
我在想是否有办法动态定义键,或者在读取文档时不处理所有键,而是按需加载它们。或者由于定义.pic
将在同一文档中,因此无法避免错误。
欢迎任何想法和建议。
平均能量损失
要了解我在说什么,请检查下面的代码。
我正在做的是创建几个带有的svg
键.pic
。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{svg.path}
\tikzset{
cog/.pic = {
\fill[scale=0.004em] svg {M863.24,382.771l-88.759-14.807c-6.451-26.374-15.857-51.585-28.107-75.099l56.821-70.452c12.085-14.889,11.536-36.312-1.205-50.682l-35.301-39.729c-12.796-14.355-34.016-17.391-50.202-7.165l-75.906,47.716c-33.386-23.326-71.204-40.551-112-50.546l-14.85-89.235c-3.116-18.895-19.467-32.759-38.661-32.759h-53.198c-19.155,0-35.561,13.864-38.608,32.759l-14.931,89.263c-33.729,8.258-65.353,21.588-94.213,39.144l-72.188-51.518c-15.558-11.115-36.927-9.377-50.504,4.171l-37.583,37.61c-13.548,13.577-15.286,34.946-4.142,50.504l51.638,72.326c-17.391,28.642-30.584,60.086-38.841,93.515l-89.743,14.985C13.891,385.888,0,402.24,0,421.435v53.156c0,19.193,13.891,35.547,32.757,38.663l89.743,14.985c6.781,27.508,16.625,53.784,29.709,78.147L95.647,676.44c-12.044,14.875-11.538,36.312,1.203,50.669l35.274,39.73c12.797,14.382,34.028,17.363,50.216,7.163l77-48.37c32.581,22.285,69.44,38.664,108.993,48.37l14.931,89.25c3.048,18.896,19.453,32.76,38.608,32.76h53.198c19.194,0,35.545-13.863,38.661-32.759l14.875-89.25c33.308-8.147,64.531-21.245,93.134-38.5l75.196,53.705c15.53,11.155,36.915,9.405,50.478-4.186l37.598-37.597c13.532-13.536,15.365-34.893,4.127-50.479l-53.536-75.059c17.441-28.738,30.704-60.238,38.909-93.816l88.758-14.82c18.921-3.116,32.756-19.469,32.756-38.663v-53.156C895.998,402.24,882.163,385.888,863.24,382.771zM449.42,616.013c-92.764,0-168-75.25-168-168c0-92.764,75.236-168,168-168c92.748,0,167.998,75.236,167.998,168C617.418,540.763,542.168,616.013,449.42,616.013z};
},
monitor/.pic = {
\fill[scale=0.01em] svg {M145.407,248.988l-2.775,26.209c-0.224,2.115,0.46,4.225,1.883,5.806c1.422,1.581,3.449,2.483,5.576,2.483h79.301c2.126,0,4.153-0.903,5.576-2.483c1.422-1.581,2.106-3.691,1.883-5.806l-2.775-26.209h98.777c15.374,0,27.881-12.507,27.881-27.881V27.881C360.733,12.507,348.225,0,332.852,0H40.63C25.256,0,12.749,12.507,12.749,27.881v193.226c0,15.374,12.507,27.881,27.881,27.881H145.407z M221.055,268.487h-62.629l2.064-19.499h58.5L221.055,268.487z M40.63,15h292.222c7.103,0,12.881,5.778,12.881,12.881V197.99H27.749V27.881C27.749,20.778,33.527,15,40.63,15z M27.749,221.107v-8.117h317.983v8.117c0,7.103-5.778,12.881-12.881,12.881H40.63C33.527,233.988,27.749,228.21,27.749,221.107z};
},
notepad/.pic = {
\fill svg {M29.334,3H25V1c0-0.553-0.447-1-1-1s-1,0.447-1,1v2h-6V1c0-0.553-0.448-1-1-1s-1,0.447-1,1v2H9V1c0-0.553-0.448-1-1-1S7,0.447,7,1v2H2.667C1.194,3,0,4.193,0,5.666v23.667C0,30.806,1.194,32,2.667,32h26.667C30.807,32,32,30.806,32,29.333V5.666C32,4.193,30.807,3,29.334,3z M30,29.333C30,29.701,29.701,30,29.334,30H2.667C2.299,30,2,29.701,2,29.333V5.666C2,5.299,2.299,5,2.667,5H7v2c0,0.553,0.448,1,1,1s1-0.447,1-1V5h6v2c0,0.553,0.448,1,1,1s1-0.447,1-1V5h6v2c0,0.553,0.447,1,1,1s1-0.447,1-1V5h4.334C29.701,5,30,5.299,30,5.666V29.333z};
},
}
\begin{document}
\begin{tikzpicture}
\pic at (0,0) {cog};
\pic at (2,0) {monitor};
\pic at (4,0) {notepad};
\end{tikzpicture}
\end{document}
当前的问题
lualatex
如果我使用而不是,则可以添加库pdflatex
,无论使用的数量是多少\pic
。我的假设是,当它试图将所有定义加载到内存中时,内存会耗尽。因此,我想找到一种动态的方式来加载或定义图片。
由于它是用 编译的,因此不存在密钥的畸形
lualatex
。
答案1
经过一番思考,我想到如果问题出在文件太大,我可以将其分成几个文件。
我的方法是不定义任何键,当pic
要使用时,我将加载特定的文件。
我的解决方案是
\tikzset{
/tikz/pics/.unknown/.code={%
\let\currentname\pgfkeyscurrentname%
\input{\currentname.tex}%
\pgfkeysalso{\currentname/.try}%
}
}
简单来说,检查是否pic
未知并加载文件。我使用 进行了一些条件加载\IfFileExists
,但当它没有加载时,我在tikz
尝试解析失败时遇到了奇怪的错误。然后决定不使用它。
按键定义似乎有问题,因为当您pic
多次使用时,它会再次加载文件。按键定义似乎没有持久化(不确定原因)。
(任何更好或更巧妙的解决方案仍然受到欢迎
完整示例
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{svg.path}
\usepackage{filecontents}
\begin{filecontents}{cog.tex}
\tikzset{
cog/.pic = {
\fill[scale=0.004em] svg {M863.24,382.771l-88.759-14.807c-6.451-26.374-15.857-51.585-28.107-75.099l56.821-70.452c12.085-14.889,11.536-36.312-1.205-50.682l-35.301-39.729c-12.796-14.355-34.016-17.391-50.202-7.165l-75.906,47.716c-33.386-23.326-71.204-40.551-112-50.546l-14.85-89.235c-3.116-18.895-19.467-32.759-38.661-32.759h-53.198c-19.155,0-35.561,13.864-38.608,32.759l-14.931,89.263c-33.729,8.258-65.353,21.588-94.213,39.144l-72.188-51.518c-15.558-11.115-36.927-9.377-50.504,4.171l-37.583,37.61c-13.548,13.577-15.286,34.946-4.142,50.504l51.638,72.326c-17.391,28.642-30.584,60.086-38.841,93.515l-89.743,14.985C13.891,385.888,0,402.24,0,421.435v53.156c0,19.193,13.891,35.547,32.757,38.663l89.743,14.985c6.781,27.508,16.625,53.784,29.709,78.147L95.647,676.44c-12.044,14.875-11.538,36.312,1.203,50.669l35.274,39.73c12.797,14.382,34.028,17.363,50.216,7.163l77-48.37c32.581,22.285,69.44,38.664,108.993,48.37l14.931,89.25c3.048,18.896,19.453,32.76,38.608,32.76h53.198c19.194,0,35.545-13.863,38.661-32.759l14.875-89.25c33.308-8.147,64.531-21.245,93.134-38.5l75.196,53.705c15.53,11.155,36.915,9.405,50.478-4.186l37.598-37.597c13.532-13.536,15.365-34.893,4.127-50.479l-53.536-75.059c17.441-28.738,30.704-60.238,38.909-93.816l88.758-14.82c18.921-3.116,32.756-19.469,32.756-38.663v-53.156C895.998,402.24,882.163,385.888,863.24,382.771zM449.42,616.013c-92.764,0-168-75.25-168-168c0-92.764,75.236-168,168-168c92.748,0,167.998,75.236,167.998,168C617.418,540.763,542.168,616.013,449.42,616.013z};
}
}
\end{filecontents}
\begin{filecontents}{monitor.tex}
\tikzset{
monitor/.pic = {
\fill[scale=0.01em] svg {M145.407,248.988l-2.775,26.209c-0.224,2.115,0.46,4.225,1.883,5.806c1.422,1.581,3.449,2.483,5.576,2.483h79.301c2.126,0,4.153-0.903,5.576-2.483c1.422-1.581,2.106-3.691,1.883-5.806l-2.775-26.209h98.777c15.374,0,27.881-12.507,27.881-27.881V27.881C360.733,12.507,348.225,0,332.852,0H40.63C25.256,0,12.749,12.507,12.749,27.881v193.226c0,15.374,12.507,27.881,27.881,27.881H145.407z M221.055,268.487h-62.629l2.064-19.499h58.5L221.055,268.487z M40.63,15h292.222c7.103,0,12.881,5.778,12.881,12.881V197.99H27.749V27.881C27.749,20.778,33.527,15,40.63,15z M27.749,221.107v-8.117h317.983v8.117c0,7.103-5.778,12.881-12.881,12.881H40.63C33.527,233.988,27.749,228.21,27.749,221.107z};
}
}
\end{filecontents}
\begin{filecontents}{notepad.tex}
\tikzset{
notepad/.pic = {
\fill svg {M29.334,3H25V1c0-0.553-0.447-1-1-1s-1,0.447-1,1v2h-6V1c0-0.553-0.448-1-1-1s-1,0.447-1,1v2H9V1c0-0.553-0.448-1-1-1S7,0.447,7,1v2H2.667C1.194,3,0,4.193,0,5.666v23.667C0,30.806,1.194,32,2.667,32h26.667C30.807,32,32,30.806,32,29.333V5.666C32,4.193,30.807,3,29.334,3z M30,29.333C30,29.701,29.701,30,29.334,30H2.667C2.299,30,2,29.701,2,29.333V5.666C2,5.299,2.299,5,2.667,5H7v2c0,0.553,0.448,1,1,1s1-0.447,1-1V5h6v2c0,0.553,0.448,1,1,1s1-0.447,1-1V5h6v2c0,0.553,0.447,1,1,1s1-0.447,1-1V5h4.334C29.701,5,30,5.299,30,5.666V29.333z};
}
}
\end{filecontents}
\tikzset{
/tikz/pics/.unknown/.code={%
\let\currentname\pgfkeyscurrentname%
\input{\currentname.tex}%
\pgfkeysalso{\currentname/.try}%
}
}
\begin{document}
\begin{tikzpicture}
\pic at (0,0) {cog};
\pic at (2,0) {monitor};
\pic at (4,0) {notepad};
\end{tikzpicture}
\end{document}