2

enter image description hereHello I have the following code:

\documentclass[12pt]{article}
\usepackage[top=0.3in, bottom=1.5in, left=0.8in, right=0.8in]{geometry}

\usepackage[utf8]{inputenc}

\usepackage{setspace}
\setlength{\parindent}{0cm}

\usepackage{amsmath}
\usepackage{unicode-math}

\usepackage{listings}

\begin{document}

\begin{lstlisting}[frame=single,title={Blinking both LEDs},breaklines=true]

#include  <msp430g2553.h>

 unsigned int i = 0;

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;

  P1DIR |= 0x01;
  P1DIR |= 0x40;

  for(;;)

  {
        //Both LEDs blink simultaneously

        P1OUT ^= 0x01;
        P1OUT ^= 0x40;

        //Or alternative P1OUT ^= 0x01|0x40

        for(i=0;i<30000;i++);

        //If I place P1OUT ^= 0x40; here then one LED blink and then the other
  };

}


\end{lstlisting}

\end{document}

In the last comment why doesn't the new line starts from the beggining (meaning under the //) but instead starts with a little left margin? And also why the first comment is far more left than the rest of the code in its block?

I have uploaded an image also with tabs on. There is something wrong. When I experiment with tabs odd things happen and the document doesn't appear as it should be. The second image is based on the code I wrote in that post and I haven't experiment on that one.

enter image description here

Adam
  • 4,684
  • 3
    You must have a tab, versus actual spaces which are being treated differently. You can try adjusting tabsize=<number>. Cutting and pasting your MWE works fine for me so the tabbing must be getting lost in the pasting here. – Peter Grill Mar 19 '14 at 19:09
  • 1
    @PeterGrill The site replaces all tabs by 4 spaces. – jub0bs Mar 19 '14 at 19:15
  • 1
    For debugging this you could add showtabs=true to your listing – Andrew Swann Mar 19 '14 at 21:02
  • There is something wrong...Even though I press tab in the first comment it doesn't appear... – Adam Mar 19 '14 at 21:06
  • 1
    @Adam Try to copy the code you posted in your question, paste it back into your editor/IDE, and compile. Do you get the same output? – jub0bs Mar 19 '14 at 21:21
  • Hm no I don't! Everything appers as it should. Weird... – Adam Mar 19 '14 at 21:22
  • @Adam The reason is probably that given by Peter Grill in his earlier comment, then: your editor must insert a tab character when you hit the Tab key. In my experience, when using listings, it's safer to stick to spaces instead. Most editors/IDEs can be configured to insert spaces (e.g. 4) instead of a tab character when the Tab key is pressed. Look into it. – jub0bs Mar 19 '14 at 21:40
  • This problem you mean is fault of the LaTeX editor or the editor in which I wrote the program in C? – Adam Mar 19 '14 at 21:44
  • @Adam Well, if you only use "embedded" listings (by using an lstlisting environment, as in your MWE) as opposed to "standalone" listings (by using \lstinputlisting), the problem can be solved in your LaTeX editor alone. – jub0bs Mar 19 '14 at 21:52
  • I didn't quite understand the last comment but thank you anyway! – Adam Mar 19 '14 at 21:54
  • @Adam What I mean is that, if your document only uses lstlisting (not \lstinputlisting), you can solve the problem completely in your LaTeX editor by searching for tab characters and replacing each of them by an appropriate number of spaces (typically, 2 or 4). – jub0bs Mar 19 '14 at 22:05

0 Answers0