TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

Image 1 generated as PDF

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:

Image 2 generated as PDF

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

share|improve this question
    
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
1  
The font is set by the document class by default, unless changed. – Christian Hupfer Jan 25 at 15:20
up vote 6 down vote accepted

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}

enter image description here

share|improve this answer
    
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:

enter image description here

\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}
share|improve this answer

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}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.