我正在尝试将一些外部生成的代码放入森林环境中。对于 tikzpicture 环境,可以这样做:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{filecontents}
\begin{filecontents}{file.txt}
\node[draw, circle] {};
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\input{file.txt}
\end{tikzpicture}
\end{document}
是否有可能在森林环境中取得类似的结果?
对森林环境采用同样的方法会产生错误:
\documentclass[border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}%
\input{file.txt}%
\end{forest}
\end{document}
(这文件内容-package ist 未使用,因为它会在文件末尾添加换行符(我如何输入文件而不获取隐式换行符。
答案1
您可以借助以下工具完成此操作catchfile
:
\begin{filecontents*}{\jobname-code.tex}
% https://tex.stackexchange.com/a/295354/
for tree={
grow'=east,
math content,
edge={->,>=latex},
if={isodd(n_children())}{
calign primary child/.pgfmath={(n_children()+1)/2},
calign=child edge,
}{}
},
forked edges
[T>T^0
[T>A_f
[C_a(T-A_f) <\sigma <C_a (T-A_s)
[ {\zeta_s^n=\zeta_s^0-\frac{\zeta_s^0}{\zeta^0}(\zeta^0-\zeta^n)} ]
[ {\zeta_T^n=\zeta_T^0-\frac{\zeta_T^0}{\zeta^0}(\zeta^0-\zeta^n)} ]
[{\zeta^n=\frac{\zeta^0}{2}(cos \big (\alpha_A(T-A_s-\frac{\sigma}{C_a})\big )+1)} ]
]
[\sigma<C_a(T-A_s)
[ {\zeta^n=\zeta^{n-1}} ]
[ {\zeta_s^n=\zeta_s^{n-1}} ]
[ {\zeta_T^n=\zeta_T^{n-1}} ]
]
]
[A_s<T<A_f
[\sigma<C_a(T-A_s)
[ {\zeta_s^n=\zeta_s^0-\frac{\zeta_s^0}{\zeta^0}(\zeta^0-\zeta^n)} ]
[ {\zeta_T^n=\zeta_T^0-\frac{\zeta_T^0}{\zeta^0}(\zeta^0-\zeta^n)} ]
[{\zeta^n=\frac{\zeta^0}{2}(cos \big (\alpha_A(T-A_s-\frac{\sigma}{C_a})\big )+1)}, calign with current]
[\sigma>C_a(T-A_s)
[ {\zeta^n=\zeta^{n-1}} ]
[ {\zeta_s^n=\zeta_s^{n-1}} ]
[ {\zeta_T^n=\zeta_T^{n-1}} ]
]
]
]
[T<A_s
[ {\zeta^n=\zeta^{n-1}} ]
[ {\zeta_s^n=\zeta_s^{n-1}} ]
[ {\zeta_T^n=\zeta_T^{n-1}} ]
]
]
\end{filecontents*}
\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\usepackage{catchfile}
\newcommand{\inputforest}[2][]{%
\begingroup
\CatchFileDef{\temp}{#2}{}%
\edef\temp{\endgroup
\unexpanded{\begin{forest}#1}%
\unexpanded\expandafter{\temp}%
\unexpanded{\end{forest}}%
}\temp
}
\begin{document}
\inputforest{\jobname-code}
\end{document}
代码由 cfr 提供。如果您将其称为\inputforest[<some forest code>]{file}
,<some forest code>
则会立即放置在文件内容之前。
一种不同的方法是action character
;感谢 cfr 的建议。
\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\usepackage{catchfile}
\newcommand{\inputforest}[2][@]{%
\begingroup
\bracketset{action character=#1}
\CatchFileDef{\temp}{#2}{}%
\begin{forest}#1\temp\end{forest}
\endgroup
}
\begin{document}
\inputforest[!]{\jobname-code}
\end{document}
这里的可选字符是“动作字符”:它应该是代码中没有出现的字符forest
,默认@
。