3

I am currently trying to insert a batch file programming code in my report. I am using the code listing. I am not able to preserve white spaces and tabs to format my code! I tried using \hspace but it didn't worked. I have following code example yet:

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

    \begin{document}

    \begin{lstlisting}[escapechar=ä]
     ä\colorbox{white}{%
      \parbox{3.9in}{\color{black}\texttt{Microsoft Windows [Version 6.2.9200]\\\\
       (c) 2012 Microsoft Corporation. All rights reserved.}}}ä
    \end{lstlisting}

    \end{document} 

The output is like this which I don't prefer:

Microsoft Windows [Version 6.2.9200]

(c) 2012 Microsoft Corporation. All rights reserved.

I prefer a tab or white space before the second line starts. The output I prefer is as below:

Microsoft Windows [Version 6.2.9200]

      (c) 2012 Microsoft Corporation. All rights reserved.

Thanks.

David Carlisle
  • 757,742
ρss
  • 281
  • Can I ask why you are putting it into a box, which means you don't get verbatim output? – Joseph Wright Aug 16 '13 at 19:03
  • I don't know why I am using box, I just wanted to add a source code I started with listing then I needed color for text, so I just mixed up box, listing etc. I am new to latex! :( Do you have better idea. All I want to do is produce the output given above in my question with text in blue color. Also with listing I can label my code so I am confused what to use? – ρss Aug 16 '13 at 19:20
  • @pss You should have asked how to get started with the listings package... – karlkoeller Aug 16 '13 at 19:34
  • listing package doesn't support Batch file code – ρss Aug 16 '13 at 19:50
  • @pss Why not? Have a look at page 13 in the listings documentation for the predefined languages. Probably command.com is the one you're looking for, and, if you can't find the one that matches your needs, you can always create your custom one following the guidelines. – karlkoeller Aug 16 '13 at 22:42

2 Answers2

6

One easy way is to add an \hphantom command:

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

\begin{document}

\begin{lstlisting}[escapechar=ä]
 ä\colorbox{white}{%
  \parbox{4.3in}{\color{black}\texttt{Microsoft Windows [Version 6.2.9200]\\\\
  \hphantom{Micros}(c) 2012 Microsoft Corporation. All rights reserved.}}}ä
\end{lstlisting}

\end{document} 

enter image description here

karlkoeller
  • 124,410
  • Thanks but how can I adjust the amount of white space? Sometimes I need more space and some times less! – ρss Aug 16 '13 at 19:16
  • @pss Inside \hphantom you can write as many characters as you want. Add or remove characters to achieve what you want (and adjust the width of the \parbox). In your case it is even easier since the characters are monospaced. – karlkoeller Aug 16 '13 at 19:23
  • \hphantom{Micros} what is this doing? I didn't get it. Thanks – ρss Aug 16 '13 at 19:51
  • @pss \hphantom{} insert horizontal space with the same size as the content of the braces. – Torbjørn T. Aug 16 '13 at 19:57
2

You can easily set the style of a listings to blue typewriter text by using \lstset{basicstyle=\ttfamily\color{blue}}. As lstlisting is a verbatim environment, all spaces are preserved.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{listings}
\lstset{basicstyle=\ttfamily\color{blue}}
\begin{document}

\begin{lstlisting}
Microsoft Windows [Version 6.2.9200]

     (c) 2012 Microsoft Corporation. All rights reserved.
\end{lstlisting}

\end{document}

Result

Heiko Oberdiek
  • 271,626
Torbjørn T.
  • 206,688
  • You've beaten me for a second. I was adding the same thing in my answer... Anyway the question is different. +1 anyway. – karlkoeller Aug 16 '13 at 19:32
  • @karlkoeller Yeah, you're right. Thought it might be useful anyway, that's why I answered. I could always delete it, if it doesn't fit. – Torbjørn T. Aug 16 '13 at 19:39
  • I agree with you. – karlkoeller Aug 16 '13 at 19:42
  • In this case how are white spaces and tabs handled? Should I simply type my text with tabs and spaces and it will show up exactly the same? Thanks – ρss Aug 16 '13 at 19:52
  • @pss Yes, verbatim means that the text comes out just as typed, including whitespace. – Torbjørn T. Aug 16 '13 at 19:56
  • Thanks, I select this answer because the approach is simple for a noob like me. Thanks to all the persons who answered! – ρss Aug 16 '13 at 20:01
  • @TorbjørnT. T. Can I change the font to consolas font type? – ρss Aug 16 '13 at 20:18
  • 1
    @pss That is quite a different question actually. Yes, you can. If Inconsolata is good enough, add \usepackage{inconsolata}. If you need Consolas, the easiest is if you can use either xelatex or lualatex to compile your document, and replace \usepackage[utf8]{inputenc} with \usepackage{fontspec}\setmonofont{Consolas}. The fontspec package, which can only be used with xelatex and lualatex, allows you to use any TrueType/OpenType font installed in your system. – Torbjørn T. Aug 16 '13 at 20:29
  • I added the package you mentioned but do I need to specify the font type somewhere like in here: \lstset{basicstyle=\ttfamily\color{blue}} ? – ρss Aug 16 '13 at 20:32
  • @pss No, the inconsolata package changes the monospace font to Inconsolata, and \ttfamily means that the monospace font is used. If you're interested in a little more about the other option fontspec, you can take a look at http://tex.stackexchange.com/a/37251/586 – Torbjørn T. Aug 16 '13 at 20:35