6

I'm trying to put the output of the tree command in a lstlisting using the following code

   \documentclass[10pt,a4paper,compress,svgnames]{beamer}
    \begin{document}
    \usepackage{listings}
    \begin[fragile]{frame}
        \begin{lstlisting}
        5053
        ├── 20141018105053_100_samples.bin
        ├── 20141018105053_101_samples.bin
        ├── 20141018105053_102_samples.bin
        ├── 20141018105053_105_samples.bin
        └── out
            ├── 20141018105053_100_samples.bin.pps
            ├── 20141018105053_101_samples.bin.pps
            ├── 20141018105053_102_samples.bin.pps
            ├── 20141018105053_105_samples.bin.pps
            └── TOA_train.npy
        \end{lstlisting}
    \end{frame}     
    \end{document}

but I'm getting this output enter image description here How can I make the lines appear on the output document?

7

The output of tree on your machine uses special UTF-8 characters , , to draw the lines. On the one hand, these characters are not available in the standard LaTeX fonts. On the other hand, listings itself is not capable to typeset multibyte characters.

To include the output "as is" in you LaTeX document, a simple solution would be to use listings literate option to transparently transform these characters into something that listings can typeset:

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage{listings}
  \lstdefinestyle{ascii-tree}{
    literate={├}{|}1 {─}{--}1 {└}{+}1 
  }
\begin{document}
\begin{frame}[fragile]

\begin{lstlisting}[style=ascii-tree]
5053
├── 20141018105053_100_samples.bin
├── 20141018105053_101_samples.bin
├── 20141018105053_102_samples.bin
├── 20141018105053_105_samples.bin
└── out
    ├── 20141018105053_100_samples.bin.pps
    ├── 20141018105053_101_samples.bin.pps
    ├── 20141018105053_102_samples.bin.pps
    ├── 20141018105053_105_samples.bin.pps
    └── TOA_train.npy
\end{lstlisting}

\end{frame}     
\end{document}

enter image description here

This basically mimics the tree output on a terminal that does not feature UTF-8. However, you could substitute with whatever you want (even macros), so the output could be improved even further. In the following, I replace them by some (plain) line drawing commands:

  \lstdefinestyle{tree}{
    literate=
    {├}{{\smash{\raisebox{-1ex}{\rule{1pt}{\baselineskip}}}\raisebox{0.5ex}{\rule{1ex}{1pt}}}}1 
    {─}{{\raisebox{0.5ex}{\rule{1.5ex}{1pt}}}}1 
    {└}{{\smash{\raisebox{0.5ex}{\rule{1pt}{\dimexpr\baselineskip-1.5ex}}}\raisebox{0.5ex}{\rule{1ex}{1pt}}}}1 
  }
\begin{document}
\begin{frame}[fragile]

\begin{lstlisting}[style=tree]
5053
├── 20141018105053_100_samples.bin
├── 20141018105053_101_samples.bin
├── 20141018105053_102_samples.bin
├── 20141018105053_105_samples.bin
└── out
    ├── 20141018105053_100_samples.bin.pps
    ├── 20141018105053_101_samples.bin.pps
    ├── 20141018105053_102_samples.bin.pps
    ├── 20141018105053_105_samples.bin.pps
    └── TOA_train.npy
\end{lstlisting}

\end{frame}     

Which results in:

enter image description here

  • For some reason, I can't get it to work with LuaLaTeX – Antoine Bolvy May 5 '17 at 19:12
  • LuaLaTex directly supports UTF8, so it might be enough to just include the tree output directly into a verbatim environment. – Daniel May 5 '17 at 20:40
  • Unfortunately no, even in a verbatim environment I have about the same output as OP in its original question. I might end up posting a new question for this unfortunately. – Antoine Bolvy May 5 '17 at 20:43
  • It does not work under Xelatex, this is the same issue as @AntoineBolvy said. I'm not sure why. – ollydbg23 Jan 29 '18 at 4:17
  • BTW: I just tried the pdflatex command, it works fine. – ollydbg23 Jan 29 '18 at 4:21
6

May be you should use dirtree package:

   \documentclass[10pt,a4paper,compress,svgnames]{beamer}
   \usepackage{dirtree}
    \begin{document}
    \begin{frame}
        \dirtree{%
        .1 5053 .
        .2 20141018105053\_100\_samples.bin.
        .2 20141018105053\_101\_samples.bin.
        .2 20141018105053\_102\_samples.bin.
        .2 20141018105053\_105\_samples.bin.
        .2 out .
            .3 20141018105053\_100\_samples.bin.pps.
            .3 20141018105053\_101\_samples.bin.pps.
            .3 20141018105053\_102\_samples.bin.pps.
            .3 20141018105053\_105\_samples.bin.pps.
            .3 TOA\_train.npy.
        }
    \end{frame}
    \end{document}

The package does not seem to be included in mainstream Latex distribution but it can be downloaded from CTAN here. enter image description here

  • Just tried in article class, works fine (but requires manual installation in my case). And a lot easier than the accepted answer! ;-) – kebs Oct 12 '16 at 15:20
0

just came across this topic in my latex document too. The solution from @Daniel just worked partially. I had to add two more lines for replacing treeisch output.

\lstdefinestyle{tree}{
literate=
    {├}{{\smash{\raisebox{-1ex}{\rule{1pt}{\baselineskip}}}\raisebox{0.5ex}{\rule{1ex}{1pt}}}}1 
    {│}{{\smash{\raisebox{-1ex}{\rule{1pt}{\baselineskip}}}\raisebox{0.5ex}{\rule{1ex}{0pt}}}}1
    { }{~}1
    {─}{{\raisebox{0.5ex}{\rule{1.5ex}{1pt}}}}1 
    {└}{{\smash{\raisebox{0.5ex}{\rule{1pt}{\dimexpr\baselineskip-1.5ex}}}\raisebox{0.5ex}{\rule{1ex}{1pt}}}}1
}

The vertical line and the "space" was missing which gave me an error during compiling. I hope that this answer will help people who are not familiar with LaTeX. Enjoy.

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.