I was writing an essay to analyze a text-based game for a class assignment. The game has an Easter egg of saving a text file locally in the computer, and there is a text-based visual art in it (guess what game it is!). I want to show the art in a figure. How can I do that?

After messing around with some packages, I finally found the way to use the verbatimbox package to draw a text art:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
\documentclass{article}

\usepackage{verbatimbox}
\usepackage{varwidth}
\usepackage{graphicx}

\begin{filecontents}[noheader,overwrite]{art.txt}
  ______________
< Kat loves Mar. >
  --------------
         \   ^__^ 
          \  (oo)\_______
             (__)\       )\/\\
                 ||----w |
                 ||     ||
\end{filecontents}

\begin{filecontents}[noheader,overwrite]{wide-art.txt}
  ______________          ______________          _______________________
< Kat loves Mar. >      < Mar loves Kat. >      < They live a happy life. >
  --------------          --------------          -----------------------
         \   ^__^                \   ^__^                \   ^__^ 
          \  (oo)\_______         \  (oo)\_______         \  (oo)\_______
             (__)\       )\/\\       (__)\       )\/\\       (__)\       )\/\\
                 ||----w |               ||----w |               ||----w |
                 ||     ||               ||     ||               ||     ||
\end{filecontents}

\begin{document}

\begin{figure}[h!]
	\centering
	\begin{varwidth}{\linewidth}
		\verbfilenobox[\fontsize{10pt}{10pt}\selectfont]{art.txt}
	\end{varwidth}
	\caption{A text-based visual art.}
\end{figure}

\begin{figure}[h!]
	\resizebox{\linewidth}{!}{\vbox{\verbfilenobox[\fontsize{10pt}{10pt}\selectfont]{wide-art.txt}}}
	\caption{A wider text-based visual art.}
\end{figure}

\end{document}

If the art is not wide enough to exceed the line width, use the first approach. The package varwidth is needed for the varwidth environment. Otherwise, use the second approach. The package graphicx is needed for the \resizebox command.

The command \fontsize{10pt}{10pt} is so that there is no baseline skip between lines. Usually the baseline skip messes with text-based arts.

If the art is large, I recommend using LuaLaTeX. Otherwise, you may encounter the error TeX capacity exceeded, sorry.

End result:

Rendered result of the sample LaTeX code.