Skip to main content
added 464 characters in body
Source Link
Display Name
  • 49.7k
  • 52
  • 196
  • 352

My answer will not answer your question. :-) But I have a good way to manage code snippet inclusion to be easily accessible for the readers. Rather than copying the code from the pdf, it will be better if you allow the readers to open the underlying code file by clicking hyperlinks.

Try compile the following code. I use latex-dvips-ps2pdf compilation steps. You can use pdflatex of course, but don't forget to remove dvips option in \documentclass.

For the sake of easy maintainability, create your own package as follows:

For the sake of easy maintainability, create your own package as follows:

My answer will not answer your question. :-) But I have a good way to manage code snippet inclusion to be easily accessible for the readers. Rather than copying the code from the pdf, it will be better if you allow the readers to open the underlying code file by clicking hyperlinks.

Try compile the following code. I use latex-dvips-ps2pdf compilation steps. You can use pdflatex of course, but don't forget to remove dvips option in \documentclass.

For the sake of easy maintainability, create your own package as follows:

added 322 characters in body
Source Link
Display Name
  • 49.7k
  • 52
  • 196
  • 352
\documentclass[dvips,dvipsnames,cmyk,12pt]{article} \usepackage[a4paper,margin=2cm]{geometry} \usepackage{xport} \begin{document} \IncludeCSharp[style=A]{Codes/CSharp/MyCode/Program.cs} \end{document} 

The imported C# file is named Program.cs in Codes/ sub directory.

using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!");//This is a comment. /*This is a comment too.*/ } } } 
\documentclass[dvips,dvipsnames,cmyk,12pt]{article} \usepackage[a4paper,margin=2cm]{geometry} \usepackage{xport} \begin{document} \IncludeCSharp[style=A]{Codes/CSharp/MyCode/Program.cs} \end{document} 
\documentclass[dvips,dvipsnames,cmyk,12pt]{article} \usepackage[a4paper,margin=2cm]{geometry} \usepackage{xport} \begin{document} \IncludeCSharp[style=A]{Codes/Program.cs} \end{document} 

The imported C# file is named Program.cs in Codes/ sub directory.

using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!");//This is a comment. /*This is a comment too.*/ } } } 
Source Link
Display Name
  • 49.7k
  • 52
  • 196
  • 352

For the sake of easy maintainability, create your own package as follows:

\NeedsTeXFormat{LaTeX2e}[1994/06/01] \ProvidesPackage{xport}[2011/06/05 v0.01 LaTeX package for my own purpose] \RequirePackage[utf8x]{inputenc} \RequirePackage{xcolor} \RequirePackage{caption} \captionsetup {% font={small,rm}, labelfont={color=Maroon,bf}, justification=justified% } \RequirePackage{listings} \AtBeginDocument {% \renewcommand*{\lstlistlistingname}{Code List}% \renewcommand*{\lstlistingname}{Code}% } \lstset {% %linewidth=\linewidth, breaklines=true, tabsize=3, showstringspaces=false% } \lstdefinestyle{Common} {% extendedchars=\true, language={[Sharp]C}, %alsolanguage={PSTricks}, frame=single, %=========================================================== framesep=3pt,%expand outward. framerule=0.4pt,%expand outward. xleftmargin=3.4pt,%make the frame fits in the text area. xrightmargin=3.4pt,%make the frame fits in the text area. %=========================================================== rulecolor=\color{Red}% } \lstdefinestyle{A}% {% style=Common, backgroundcolor=\color{Yellow!10}, basicstyle=\scriptsize\color{Black}\ttfamily, keywordstyle=\color{Orange}, identifierstyle=\color{Cyan}, stringstyle=\color{Red}, commentstyle=\color{Green}% } \newcommand{\IncludeCSharp}[2][style=A]% {% \lstinputlisting[#1,caption={\href{#2}{#2}}]{#2}% } %for beamer, use \hypersetup instead. \RequirePackage[colorlinks=true,bookmarksnumbered=true,bookmarksopen=true]{hyperref} %to make hyperlinks point to the top of figure or table. %it must be loaded after hyperref. \RequirePackage[all]{hypcap}% cannot be used in beamer. \endinput 

In the main input file, you can call \IncludeCSharp as follows:

\documentclass[dvips,dvipsnames,cmyk,12pt]{article} \usepackage[a4paper,margin=2cm]{geometry} \usepackage{xport} \begin{document} \IncludeCSharp[style=A]{Codes/CSharp/MyCode/Program.cs} \end{document}