Archive

Archive for the ‘latex’ Category

[latex] using accents

Problem
I wanted to write a LaTeX document in Hungarian using those funny accents (áéíóöőüű), but they didn’t appear in the PDF output.

Solution

\documentclass[hungarian]{article}
\usepackage{babel}
\usepackage[utf8]{inputenc}


The tip is from here.

Categories: latex Tags: , ,

pretty print a C# source code

Problem
I wanted to print the page at https://learnxinyminutes.com/docs/csharp/, but the produced PDF was black and white, i.e. the syntax highlighting was lost. How to print a source code with colors?

Solution
I tried several browsers, several editors, but nothing worked. Then I found a LaTeX solution. The necessary files and the produced PDF output are here: https://github.com/jabbalaci/blog-assets/tree/master/20180329-print-CSharp-code.

I put here the LaTeX source:

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage[usenames,dvipsnames]{color}  %% Allow color names

%% The listings package will format your source code
\usepackage{listings}
\lstdefinestyle{customasm}{
belowcaptionskip=1\baselineskip,
xleftmargin=\parindent,
language={[Sharp]C},
breaklines=true, %% Wrap long lines
basicstyle=\footnotesize\ttfamily,
stringstyle=\color{Black},
keywordstyle=\bfseries\color{OliveGreen},
identifierstyle=\color{blue},
xleftmargin=-8em,
showstringspaces=false
}
\begin{document}

\lstinputlisting[style=customasm]{src.cs}

\end{document}


The LaTeX tip is from here.

Categories: csharp, latex Tags:

LaTeX: horizontal line

\newcommand\sep{\noindent\rule[0.5ex]{\linewidth}{1pt}}


Usage:

text

\sep

go on


LaTeX: strike through a text

Problem
You want to have this effect in LaTeX.

Solution

\usepackage{soul}

You want to have \st{this effect} in \latex.

Categories: latex Tags:

Merge PDF files and start each on the right side (on an add page)

Problem
I had several small PDF files that I wanted to print in one shot. With pdftk you can concatenate them easily but I wanted to see each document to start on an odd page, i.e. on the right side. How to do that?

Solution
A hackish solution is to use pdftk. Check the number of pages and if it’s odd, insert a blank page. Then merge ’em all.

A better way is to leave the job for LaTeX:

\documentclass[a4paper]{article}
\usepackage{pdfpages}

\begin{document}

\includepdf[pages={-}, openright]{01.pdf}
\includepdf[pages={-}, openright]{02.pdf}
\includepdf[pages={-}, openright]{03.pdf}
%...

\end{document}


Compile the source with “pdflatex”. Warning! The part “pages={-}” means to insert the whole PDF, from start to end. If you forget that, it’ll insert just the 1st page of a PDF.

Tip from here and here.

Categories: latex

minted – highlighted source code for LaTeX

minted is a package that facilitates expressive syntax highlighting in LaTeX using the powerful Pygments library. The package also provides options to customize the highlighted source code output using fancyvrb.

PDF doc is here.

Example (minimal.tex):

\documentclass{article}
\usepackage{minted}    % import the package here
\begin{document}
\begin{minted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}
\end{document}


Compilation:

\$ pdflatex -shell-escape minimal


Alternatives

• \begin{verbatim} ... \end{verbatim}
• \begin{lstlisting} ... \end{lstlisting}
Categories: latex, python

Squeezing Space in LaTeX

Problem
You have written a nice paper that is 17 pages long but the limit is 16 pages. You try to remove some parts of it but it’s still 16.5 pages long. What to do?

Solution
\linespread{0.9}