3

I'm trying to put some externally produced code into a forest environment. For the tikzpicture environment it's possible like this:

\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}

Is it possible to achieve a similar result with the forest environment?

The same approach with a forest environment yields an error:

\documentclass[border=10pt]{standalone}
\usepackage{forest}

\begin{document}
  \begin{forest}%
   \input{file.txt}%
  \end{forest}
\end{document}

(the filecontents-package ist not used because it adds a newline to the end of a file (how-can-i-input-a-file-without-getting-the-implicit-newline.)

  • What's in the external file in the Forest case? – cfr Jul 26 '17 at 0:59
  • @cfr a "raw" tree in its bracket representation. Just something like [2[1][1]] (on multiple lines). – Imperaton Jul 26 '17 at 6:44
  • It is always better to give a complete example in the question i.e. include a sample file you want to be able to input. – cfr Jul 26 '17 at 9:17
4

You can do it with the help of 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}

Code courtesy of cfr. If you call it as \inputforest[<some forest code>]{file}, <some forest code> would be placed immediately before the file’s contents.

enter image description here

A different approach is with action character; thanks to cfr for suggesting it.

\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}

Here the optional character is the “action character”: it should be one that doesn't appear in the forest code, default @.

  • (+1, of course) But I think it would be simpler to use action character. – cfr Jul 26 '17 at 9:16
  • @cfr Thanks; I'm not really convinced it's better, because one needs to choose a character that doesn't appear in the code. – egreg Jul 26 '17 at 9:30
  • 1
    That's true. I believe that's why there's no default action character. – cfr Jul 26 '17 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.