TransWikia.com

Output from tree command in a listing

TeX - LaTeX Asked by Pedro G. on June 30, 2021

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?

4 Answers

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}{dimexprbaselineskip-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

Correct answer by Daniel on June 30, 2021

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

Answered by user11232 on June 30, 2021

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}{dimexprbaselineskip-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.

Answered by Tobi on June 30, 2021

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}{dimexprbaselineskip-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.

Answered by Aldo D'Aquino on June 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP