9

I need to write a whole bunch of pseudo code and I got annoyed using all the "\ >\=" with the \begin{tabbing} command, so I decided to use \begin{verbatim} instead. This was working fine for me until I got to a point in which I had to indent 4 times in a row. When I get to 4 tabs in a row it puts the "character" >> at the beginning of my line and then when I generate the pdf it is not indented at all. How do I get latex to stop reformatting my lines when I use verbatim?

\begin{verbatim}

makeClusters(nodes[n], k)
{
distances [n][n]
clusters[k]
seeds[k]
numseeds=0
maxd=0
node1
node2

if(k=0)
  return

if(k=1)
  Put all nodes in one cluster and return

for i=0 to  n-1
  for j=0 to n-1
    distances[i][j] = d(nodes[i],nodes[j])
    if(distances[i][j]>maxd)
      maxd=distances[i][j]
      node1=i
      node2=j

seeds[0]=node1
seeds[1]=node2
numseeds +=2

while(numseeds < k)
  minNode
  maxmin=0

  for i=0 to n-1
    for j=0 to numseeds-1
      mindis=maxd
      if(distances[i][seeds[j]]<mindis)
    mindis=distances[i][seeds[j]]

    if(mindis>maxmin)
      maxmin=mindis
      minNode=i

  seeds[numseeds]=minNode
  numseeds++

for i=0 to n-1
  mindis=maxd
  clust
  if(not inCluster(node[i]))
    for j=0 to n-1
      if(inCluster(node[j]) and distances[i][j]<mindis)
    mindis=distances[i][j]
    clust=cluster(node[j])
    add2cluster(clusters[clust],node[i]


}
\end{verbatim}

As you can see in this unnecessarily large example after 2 of my if statements the lines have been unindented, these are the locations where I used 4 tabs and have copy pasted this section directly out of Kile.

3
  • 2
    This sounds like a job for the listings package, really. Could you post an example of your code?
    – Joseph Wright
    Oct 6, 2010 at 21:10
  • So, the problem is actually your editor (Kile) that for some reason messes with your tabs?
    – Caramdir
    Oct 6, 2010 at 21:44
  • @JosephWright Yes, it is better to using listings. But if the code contains unicode characters, it is a problem! Like this one
    – Say OL
    Jan 8, 2015 at 7:23

1 Answer 1

5

The problem is some rather strange (to me) behavior of Kile: When you press the [Tab] key, it inserts two spaces into the document instead of a tab. But it also replaces any 8 consecutive spaces by a tab character. This obviously completely messes up any formatting.

To fix this problem either use an editor with sane default settings or go to "Settings" > "Editing" > "General" and check "Insert spaces instead of tabulators" (or fiddle around with the settings under "Indentation").

NB: As Joseph mentioned, the listings package will give you much nicer code listings.

3
  • Does this mean that latex ignores tab characters?
    – Sled
    Mar 22, 2014 at 22:05
  • @ArtB In normal text, TeX treats all whitespace (spaces, tabs, newlines) exactly the same.
    – Caramdir
    Mar 27, 2014 at 22:05
  • @Caramdir It is right. Not only in Kile editor but also TeXstudio. When I use key tab, it is not known by verbatim environment! So we should use key space.
    – Say OL
    Jan 8, 2015 at 7:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .