10

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?

ollydbg23
  • 1,198
Pedro G.
  • 203

4 Answers4

13

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

Daniel
  • 37,517
9

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

kebs
  • 859
  • 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
  • ... but of course, it can't work if you want to input a text file generated with the tree command. – kebs Oct 14 '20 at 13:47
2

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.

Tobi
  • 447
1

This worked for me on Overleaf, the previous examples left some missing characters.

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

The order of the lines matter.