# ASCII + Unicode art in LaTeX lstlisting

I have a small ASCII diagram in my LaTeX document.

\lstset{literate={↓}{$\downarrow{}$}{1}}
\begin{lstlisting}
-1---2----3-----|
---s---------s--|
↓         ↓
---1*--------3***|
\end{lstlisting}


Adding the down arrow was hard enough already, but now the --- on the second line is converted to a horizontal line, and to larger minus signs on the bottom line. Especially the last line is wider than it should be.

I have another one that looks even worse:

\begin{lstlisting}
-4--5--7--3----|
---s--s--s--s--|
↓  \-↓   \-↓
---4****5*****3***|
\end{lstlisting}


The entire bottom line is misaligned:

How do I make it look more like the original ASCII?

-
You're using a proportional font – Christian Hupfer Jan 25 at 15:14
@ChristianHupfer Haven't set the font anywhere. But that could very well be the problem. – Dorus Jan 25 at 15:18
The font is set by the document class by default, unless changed. – Christian Hupfer Jan 25 at 15:20

The characters in most fonts do not have the same width, i.e. a 't' might be narrower than the letter a, the character width of the fonts are proportional then.

If all characters should have the same width, use a non-proportional font, such as typewriter font (e.g. Courier), which is enabled, for example with \ttfamily.

In addition I suggest to use the inputenc package to provide support for direct input of unicode characters (or use xelatex then) to make UnicodeArt ;-)

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\lstset{basicstyle={\ttfamily},literate={↓}{$\downarrow{}$}{1}}

\begin{document}

\begin{lstlisting}
1---2----3-----|
---s---------s--|
↓         ↓
---1*--------3***|
\end{lstlisting}

\begin{lstlisting}
---s---------s--|
↓         ↓
---1*--------3***|
\end{lstlisting}
\end{document}


-
Ooh that easy? It works now! – Dorus Jan 25 at 15:18
Interesting how the horizontal alignment is off slightly... – Werner Jan 25 at 22:51

listings is a viable option, but there are some minor alignment issues. Here are two alternatives using fancyvrb and a regular verbatim environment:

\documentclass{article}
\usepackage{listings,fancyvrb}
\newcommand{\da}{% down arrow
\phantom{x}%
\makebox[0pt][r]{$\downarrow$}}
\begin{document}

\begin{lstlisting}[basicstyle=\ttfamily,literate={↓}{$\downarrow{}$}{1}]
---s---------s--|
↓         ↓
---1*--------3***|
\end{lstlisting}

\begin{Verbatim}[commandchars=\\\{\}]
---s---------s--|
\da         \da
---1*--------3***|
\end{Verbatim}

\begin{verbatim}
---s---------s--|
|         |
v         v
---1*--------3***|
\end{verbatim}

\end{document}

-

Run with xelatex or lualatex:

\documentclass{article}
\usepackage{fontspec}
\setmonofont{DejaVu Sans Mono}
\usepackage{fancyvrb}
\begin{document}

\begin{Verbatim}[fontsize=\small]
1---2----3--------|
---s---------s---|
↓         ↓
---1*--------3***|
\end{Verbatim}

\bigskip
\begin{Verbatim}[fontsize=\small]
----s---------s---|
↓         ↓
----1*--------3***|
\end{Verbatim}

\end{document}


and if you prefer pdflatex then use

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{fancyvrb}
\usepackage{newunicodechar}
\newunicodechar{↓}{$\downarrow$}
\begin{document}
[...]
\end[document}


-