8

Question : How can I insert a txt file ( a source code) as such in latex without any change in indentation, tabs, formatting etc?

Scenario :

Consider I have a text file as below ( It is a part of a VHDL code edited in gEdit ):

enter image description here

I want to include this code in Latex as it is.

I used verbatim method available in Kile, and below is the result I got :

enter image description here

You can see the tabs and indentation is dislocated. ( This is just a small part, remaining part is also similar, means, everything dislocated)

How can I add this code as such into a latex file ?

EDIT :

I also used listings package. Again the same problem. Intendation is not correct. Also, it even cut some portions of text on the right side.

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity TOP_MODULE is
    port (CLK          : IN STD_LOGIC;                   -- clock
          RESET        : IN STD_LOGIC;                   -- reset signal
          DECODED_DATA : OUT STD_LOGIC_VECTOR(0 TO 15)   -- decoded message
          );
end TOP_MODULE;
4
  • 3
    Please add an minimal working example (MWE) that shows, how you have called and configured package listings.
    – Mensch
    Commented Nov 11, 2012 at 18:45
  • 1
    I think the verbatim display should be correct, the issue is with the indentation of your file : two lines on the same level of indentation should have the same number of white spaces and tabulations in front of them. If in doubt, use only white spaces and no tabulations, and you should be fine. If I'm wrong, could you please add the content of the text file (as text, not as image) in your question?
    – T. Verron
    Commented Nov 11, 2012 at 18:52
  • @T.Verron : added the text Commented Nov 11, 2012 at 19:01
  • 1
    Please add a complete runnable document that shows the problem. You have shown input and output as images which makes it impossible to reproduce. It should work with listings, the listings doc in section 2.5 that you should be able to set its interpretation of tab character with tabsize=8 for example. Commented Nov 11, 2012 at 19:50

1 Answer 1

6

You probably have tabs in your input file. You can, however, fix it without changing your input file, with either fancyvrb or listings; both packages allow also for making the type size smaller, so that the code fits in the margin.

\documentclass{article}
\usepackage{fancyvrb} % for simple solution
\usepackage{listings,color} % for colored solution

% for colored solution
\lstnewenvironment{VHDLlisting}[1][]
  {\lstset{
    language=VHDL,
    basicstyle=\footnotesize\ttfamily,
    columns=flexible,
    keepspaces=true,
    keywordstyle=\color{red},
    identifierstyle=\color{green},
    commentstyle=\color{blue},
  }}{}

\begin{document}

% simple solution (no colors)

\begin{Verbatim}[tabsize=8,fontsize=\footnotesize]
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity TOP_MODULE is
    port (CLK          : IN STD_LOGIC;                   -- clock
          RESET        : IN STD_LOGIC;                   -- reset signal
          DECODED_DATA : OUT STD_LOGIC_VECTOR(0 TO 15)   -- decoded message
      );
end TOP_MODULE;
\end{Verbatim}

% colored solution

\begin{VHDLlisting}[tabsize=8]
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity TOP_MODULE is
    port (CLK          : IN STD_LOGIC;                   -- clock
          RESET        : IN STD_LOGIC;                   -- reset signal
          DECODED_DATA : OUT STD_LOGIC_VECTOR(0 TO 15)   -- decoded message
      );
end TOP_MODULE;
\end{VHDLlisting}

\end{document}

In the example, the line that shows only ); has a tabulation character in it, just to show that the packages can cope with it. It's, to be precise,

<TAB><SP><SP>);

so that in my editor, where tabs cover 8 spaces, it is right for aligning the parentheses under the "D" in the line above it.

However, changing tabs into spaces is better: editors have different ideas about how long is a <TAB>, so what aligns in an editor may appear misaligned in another.

enter image description here

2
  • Is it possible to further reduce the font size? How? Commented Dec 5, 2012 at 6:38
  • @AbidRahmanK Use \scriptsize instead of \footnotesize
    – egreg
    Commented Dec 5, 2012 at 7:34

You must log in to answer this question.

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