Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Why does true type fonts consolidate spaces and is there any way to stop this? I need easily add white space between words equal to the width of a space(which for monospace should be the same for all letters).

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{inconsolata}
\begin{document}
\catcode`_=11
\par\texttt{this is monospaced text    Where  are  the  spaces  ?    !!!}%
\par\texttt{this_is_monospaced_text____Where__are__the__spaces__?____!!!}
\end{document}​

update:

Using \obeyspaces or \verb works BUT something seems to be ruining the spacing:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{fancyvrb}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}%
\matrix (m) [matrix of nodes, every node/.style={font=\ttfamily},%
    row 1/.style={text width=7em, align=left,font=\ttfamily},
    row 2/.style={text width=7em, align=left,font=\ttfamily},]
    {
        \verb+A  B  C  D +\\
        \verb+Ac Ad Be Cf+\\
    };
\end{tikzpicture}
\end{document}​

The output looks like the monspace font is not used.


update2:

Weird behavior: extra spaces between ? and !!!! Breaks the tikz matrix for some reason(remove tikzpicture to get it to work):

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{fancyvrb}
\usetikzlibrary{matrix}

\begin{document}


\catcode`_=11
\par\obeyspaces{\texttt{this is monospaced text    Where  are  the  spaces  ?    !!!}}%
\par\texttt{this_is_monospaced_text____Where__are__the__spaces__?____!!!}
\catcode`_=8

\begin{tikzpicture}%
\matrix (m) [matrix of nodes, every node/.style={font=\ttfamily},%
    row 1/.style={text width=7em, align=left,font=\ttfamily},
    row 2/.style={text width=7em, align=left,font=\ttfamily},]
    {
        \verb+A  B  C  D +\\
        \verb+Ac Ad Be Cf+\\
    };
\end{tikzpicture}
\end{document}​
share|improve this question
    
\obeyspaces might be what you are looking for. or more simply \verb|.. ...| –  David Carlisle Sep 3 '12 at 23:12
    
@DavidCarlisle Yep, but for some reason they don't seem to work well and causes some problems. See my second update. –  AbstractDissonance Sep 4 '12 at 0:04
1  
any command requiring catcode changes (\obeyspaces, \verb, ...) does not work in the argument to another command. But normal catcode spaces are compressed by TeX before the macro layer sees the input so you have a choice: use catcode changes or use different markup such as \ for the spaces. –  David Carlisle Sep 4 '12 at 9:29
add comment

3 Answers

up vote 3 down vote accepted

Another approach for short verbatim text is to use the \Verb command from the fancyvrb package; I added the obeytabs option and used tabsize to recover the alignment inside the \matrix (the characters are spaced using tabs):

Apparently, when I copy the code from my editor to this site the tabs get lost. You need to use A(tab)B(tab)C(tab)D and Ac(tab)Ad(tab)Be(tab)Cf in the code below, where (tab) stands for a tab in your editor:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{tikz}
\usetikzlibrary{matrix}

\fvset{obeytabs}

\begin{document}

\begin{tikzpicture}%
\matrix (m) [matrix of nodes, every node/.style={font=\ttfamily},%
    row 1/.style={text width=7em, align=left},
    row 2/.style={text width=7em, align=left}]
    {
\Verb[tabsize=2]+A  B   C   D+\\
\Verb[tabsize=1]+Ac Ad  Be  Cf+\\
    };
\end{tikzpicture}

\end{document}​

enter image description here

Using the \lstinline command from the listings package, there's no need to use tabs; simple spaces will behave as expected:

\documentclass{article}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}%
\matrix (m) [matrix of nodes, every node/.style={font=\ttfamily},%
    row 1/.style={text width=7em, align=left},
    row 2/.style={text width=7em, align=left}]
    {
\lstinline+A  B  C  D+\\
\lstinline+Ac Ad Be Cf+\\
    };
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
This works BUT spacing size doesn't seem to be correct. Seem my updated post. –  AbstractDissonance Sep 3 '12 at 23:54
    
@AbstractDissonance please see my updated answer. –  Gonzalo Medina Sep 4 '12 at 0:17
    
Using your exact code doesn't give the same results ;/ I end up with misalligned columns ;/ Not sure why. I'm using miktex 2.8 x64. –  AbstractDissonance Sep 4 '12 at 0:22
    
Did you use tabs instead of spaces to separate the different groups of letters? –  Gonzalo Medina Sep 4 '12 at 0:23
    
no, but even if I did. One line might be Ac Be Cf. So not sure if your method will work. –  AbstractDissonance Sep 4 '12 at 0:26
show 4 more comments

\texttt just sets the typeface. It doesn't change how TeX sees spaces. If you want that, then you need to use some kind of verbatim macro or environment. Or fiddle with the catcode of space (NOT RECOMMENDED!)

share|improve this answer
    
verbatim doesn't work because it seems to create a new indented paragraph. (I'm using this in a tikz matrix). Changing the catcode for space causes the space to take on a weird looking character. Verbatim does keep the extra spaces though but basically adds some vertical space to the matrix cell. –  AbstractDissonance Sep 3 '12 at 23:03
    
@AbstractDissonance Please, add to your question the setting where you need that spaces are obeyed. –  egreg Sep 3 '12 at 23:29
add comment

You can use the package listings or the environment verbatim. An example you will see in the following MWE. Could it be that a table would better fit your issue?

%http://tex.stackexchange.com/questions/70007/monospaced-fonts-not-respecting-spaces
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\begin{document}
\par\texttt{this is monospaced text    Where  is  the  spaces  ?    !!!}

\begin{verbatim}
this is monospaced text    Where  is  the  spaces  ?    !!!
\end{verbatim}

\begin{lstlisting}[basicstyle=\ttfamily]
this is monospaced text    Where  is  the  spaces  ?    !!!
\end{lstlisting}

\begin{lstlisting}[basicstyle=\ttfamily,showspaces=true]
this is monospaced text    Where  is  the  spaces  ?    !!!
\end{lstlisting}

\end{document}​
share|improve this answer
    
lstlisting seems to not work in my code(it's being used in a tikz matrix node). Verbatim seems to create a new \par that causes extra space in my matrix node. I sort of need an 'inline' verbatim. –  AbstractDissonance Sep 3 '12 at 23:10
    
Then please build a complete MWE, just loading tikZ (with all necceccary packages) and the matrix node, where you want to have monospace font. Then we can play with it to find a solution for your problem. –  Kurt Sep 3 '12 at 23:26
add comment

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.