1

After this question How to show similar display of codes of different programming languages in LaTeX which prints a code in a standalone pdf document (for instance 1000 lines of code in only one pdf page), I have problem with using that code alongside my main thesis tex file. I mean, because the document type of the codes document and my document code seems tobe different, the tex stops in the middle of compiling the tex coces. Also, it is not possible to import long codes as a figure to the main tex file (because it is larger than a standard A4 pdf file.)

How can I adjust this answer's code to be able to print it in two or three pdf pages instead of a single long pdf page?

0

Replace the \documentclass[...]{standalone} to your own \documentclass{<xxx>} where <xxx> can be replaced by any available classes such as article, book, report, etc. You will get listings that span muliple pages.

\documentclass[a4paper,11pt,twoside,openany,dvipsnames]{book}
\usepackage{xcolor}
\usepackage{listings}

\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}


\lstdefinestyle{Common}
{
    basicstyle=\scriptsize\ttfamily\null,
    numbers=left,
    numbersep=1em,
    frame=single,
    framesep=\fboxsep,
    framerule=\fboxrule,
    xleftmargin=\dimexpr\fboxsep+\fboxrule,
    xrightmargin=\dimexpr\fboxsep+\fboxrule,
    breaklines=true,
    breakindent=0pt,
    tabsize=5,
    columns=flexible,
    showstringspaces=false,
    captionpos=b,% or t for top (default)
    abovecaptionskip=0.5\smallskipamount,   % there is also belowcaptionskip
}



\lstdefinestyle{CSharp}
{
    style=Common,
    language={[Sharp]C},
    alsolanguage={[LaTeX]TeX},
    morekeywords=
    {
        % add your new fortran keywords here!
    },
}

\lstdefinestyle{CSFall}
{
    style=CSharp,   
    backgroundcolor=\color{Blue},
    basicstyle=\color{Yellow}\scriptsize\ttfamily,
    keywordstyle=\color{White}\sffamily,
    identifierstyle=\color{Cyan}\bfseries,
    commentstyle=\color{ForestGreen},
    stringstyle=\color{Maroon},
    numberstyle=\color{Black}\tiny\noaccsupp,
    rulecolor=\color{Black},
}


\lstnewenvironment{CSFall}
{\lstset{style=CSFall}}
{}




\usepackage{filecontents}
\begin{filecontents*}{csharp.cs}
public class DoubleFormatter : IFormatProvider, ICustomFormatter
{
  // always use dot separator for doubles
  private CultureInfo enUsCulture = CultureInfo.GetCultureInfo("en-US");

  public string Format(string format, object arg, IFormatProvider formatProvider)
  {
    if (arg is double)
    {
      if (string.IsNullOrEmpty(format))
      {
        // by default, format doubles to 3 decimal places
        return string.Format(enUsCulture, "{0:0.000}", arg);
      }
      else
      {
        // if user supplied own format use it
        return ((double)arg).ToString(format, enUsCulture);
      }
    }
    // format everything else normally
    if (arg is IFormattable)
      return ((IFormattable)arg).ToString(format, formatProvider);
    else
      return arg.ToString();
  }

  public object GetFormat(Type formatType)
  {
    return (formatType == typeof(ICustomFormatter)) ? this : null;
  }
}
\end{filecontents*}

\usepackage{lipsum}

\begin{document}
\lipsum[1]

\lstinputlisting[style=CSFall,caption={An example of C\# code.}]{csharp.cs}

\lipsum[2]
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.