Category: LaTeX

LaTeX: how to insert a PDF file

You can insert a PDF file in LaTeX using the \includegraphics command from the graphicx package. The basic syntax is as follows:

\usepackage{graphicx}
...
\includegraphics{file.pdf}

Where “file.pdf” is the name of your PDF file. You can also specify options such as the width and height of the image, as well as its placement on the page.

\includegraphics[width=0.8\textwidth,height=0.3\textheight,keepaspectratio,center]{file.pdf}

You can also use the pdfpages package to include an entire pdf file in the document,

\usepackage{pdfpages}
...
\includepdf[pages=-]{file.pdf}

The pdfpages package

The pdfpages package allows you to include one or more pages from a PDF file in your LaTeX document. To use the package, you first need to add it to your preamble with the following command:

\usepackage{pdfpages}

Once the package is loaded, you can use the \includepdf command to include a PDF file in your document. The basic syntax is as follows:

\includepdf[options]{file.pdf}

Where “file.pdf” is the name of the PDF file you want to include and “options” are any additional options you want to specify.

The most common option is the pages option, which allows you to specify which pages of the PDF file should be included in the document. For example, to include the first and second pages of a PDF file called “file.pdf”, you would use the following command:

\includepdf[pages={1,2}]{file.pdf}

You can also use - to include all the pages in the pdf file

\includepdf[pages=-]{file.pdf}

Other options include fitpaper, fitwindow, noautoscale, angle, scale, offset, and pagecommand. You can use these options to control the size, rotation, and placement of the included PDF pages, as well as to add custom headers and footers to the included pages.

For more details and examples on how to use the pdfpages package, you can check the package documentation.

 

How do I insert code into a LaTeX document?

There are several ways to insert code into a LaTeX document. One common method is to use the lstlisting environment from the listings package. The package provides options for formatting the code, such as language, font size, and background color. An example of how to use lstlisting to insert a piece of Python code is as follows:

\usepackage{listings}

% ...

\begin{lstlisting}[language=Python]
def hello_world():
    print("Hello, World!")
\end{lstlisting}

Another way is to use verbatim environment.

\begin{verbatim}
def hello_world():
    print("Hello, World!")
\end{verbatim}

Please note that verbatim does not format the code. You can also use other packages like minted, fancyvrb, listingsutf8 to insert code in LaTeX document.

How to use the minted package

The minted package is a LaTeX package that provides syntax highlighting for source code listings using the Pygments library. In order to use the package, you will need to have Pygments installed on your system.

To use the minted package in your LaTeX document, you will need to add the following lines to your preamble:

\usepackage{minted}

Then, you can use the minted environment to insert code listings in your document. The basic syntax is as follows:

\begin{minted}{language}
    [options]
    code
\end{minted}

Where language is the language of the code you are listing (e.g. Python, C++, Java), and options are any additional options you want to pass to the package.

For example, the following code will insert a listing of Python code with the default options:

\begin{minted}{python}
def hello_world():
    print("Hello, World!")
\end{minted}

You can also include options such as fontsize, linenos (line numbers), etc.

\begin{minted}[linenos, fontsize=\footnotesize,numbersep=5pt, frame=lines, framesep=2mm]{python}
def hello_world():
    print("Hello, World!")
\end{minted}

You also need to run a shell command to create a file with highlighted code.

pdflatex --shell-escape file.tex

Please note that minted package requires the use of an external program pygmentize to create the highlighted code, which is why it requires the --shell-escape option when running pdflatex.

How to use the fancyvrb package

The fancyvrb package is a LaTeX package that provides advanced verbatim capabilities, including support for typesetting source code listings with custom fonts and formatting.

To use the fancyvrb package in your LaTeX document, you will need to add the following lines to your preamble:

\usepackage{fancyvrb}

Then, you can use the Verbatim environment to insert code listings in your document. The basic syntax is as follows:

\begin{Verbatim}[options]
    code
\end{Verbatim}

Where options are any additional options you want to pass to the package.

For example, the following code will insert a listing of Python code with the default options:

\begin{Verbatim}
def hello_world():
    print("Hello, World!")
\end{Verbatim}

You can also include options such as fontsize, linenos (line numbers), etc.

\begin{Verbatim}[fontsize=\small,frame=single]
def hello_world():
    print("Hello, World!")
\end{Verbatim}

The fancyvrb package also provides other environments such as BVerbatim and LVerbatim which are useful for typesetting verbatim text with different formatting options.

Please note that the fancyvrb package is not recommended for large code listings because it can cause issues with line breaks and spacing. But it’s a good choice for small code snippets and text.

How to use listingsutf8 package

The listingsutf8 package is a LaTeX package that provides support for typesetting source code listings with UTF-8 encoded characters. It is an extension of the listings package and provides the same functionality as the listings package but with the added capability to handle UTF-8 encoded characters.

To use the listingsutf8 package in your LaTeX document, you will need to add the following lines to your preamble:

\usepackage{listingsutf8}

Then, you can use the lstlisting environment to insert code listings in your document. The basic syntax is as follows:

\begin{lstlisting}[options]
    code
\end{lstlisting}

Where options are any additional options you want to pass to the package.

For example, the following code will insert a listing of Python code with the default options:

\begin{lstlisting}[language=Python]
def hello_world():
    print("Hello, World!")
\end{lstlisting}

You can also include options such as fontsize, linenos (line numbers), etc.

\begin{lstlisting}[language=Python, fontsize=\small, numbers=left]
def hello_world():
    print("Hello, World!")
\end{lstlisting}

The listingsutf8 package provides many other options to customize the appearance of the code listing, such as background color, keywords, and more. It also allows for inputencoding to be UTF-8, so it can handle special characters and non-latin scripts.

Please note that if you are using an older version of LaTeX, you may need to install the listingsutf8 package manually and configure your TeX distribution to find it.

 

LaTeX: overfull \hbox mit draft sichtbar machen

LaTeX stellt einen Entwurfsmodus bereit, über den sich weitere Informationen im erzeugten Dokument anzeigen lassen. Der Modus lässt sich mit dem optionalen Argument draft wie folgt aktivieren:

1
\documentclass[draft]{...}

Dieser Modus bietet zwei Vorteile:

Übervolle Textboxen (overfull \hbox), also Textboxen welche nicht innerhalb der definierten Seitenränder umbrechen, werden durch eine schwarze Linie hervorgehoben (siehe Grafik). Diese Markierung wird bei Text, Abbildungen und auch Tabellen angezeigt.

Eingebettete Grafiken werden nicht in der Ausgabe angezeigt. Hier wird nur ein Rahmen, sowie Pfad und Name der Datei angezeigt. Dieser Effekt kann auch über das Argument draft des graphicx-Paketes erzielt werden.

1
\usepackage[draft]{graphicx}

LaTeX: d.h. oder d.\,h.? z.B. oder z.\,B.?

Grundsätzlich sollte jede Abkürzung zwischen den einzelnen Teilen ein geschütztes Leerzeichen enthalten, damit die Abkürzung durch Zeilentrennung nicht auseinandergerissen wird. In LaTeX wird das geschützte Leerzeichen mit \, gesetzt. Es sollte also heißen:

d.\,h. und z.\,B.

Laut Friedrich Forssman und Ralf de Jong – Detailtypografie:

Abkürzungen mit Binnenpunkt müssen innerhalb der Abkürzung mit einem geschützten Flexiblen Leerzeichen versehen werden. […] Nach der Abkürzung erfolgt ein normaler Wortzwischenraum.

LaTeX: hervorheben von Text mit \emph oder \textit?

Wie sollte man Text in LaTeX hervorheben? Kurz gesagt: \emph eignet sich besser als \textit und das aus zwei Gründen.

Erstens, \emph — der LaTeX Philosophie folgend, dass logisches Markup vom Inhalt getrennt werden sollte — betont den Sinn, dass es sich um eine Hervorhebung handelt, während \textit diesen Gedanken mit dem Setzen einer Schriftart (kursiv) vermischt. Entscheidet man sich im Laufe der Dokumentenerstellung die Schriftart zu wechseln (z.B. in eine schräge römische Schriftart), dann lässt sich die Anpassung mit \emph als Hervorhebung sehr einfach anpassen. Dahingegen müsste man alle Vorkommen von \textit händisch suchen und ersetzen, um den damit gekennzeichneten Text vom Rest des Dokuments abzuheben. \textit würde in dem Fall auch eher zu Verwirrungen führen.

Zweitens, und vielleicht von geringerer Bedeutung: man kann mit \emph eine Hervorhebung in der Hervorhebung erstellen. So lässt sich beispielsweise ein Wort oder eine Wortgruppe in einem Satz hervorheben, der bereits hervorgehoben wird. Die innere \emph Anweisung schaltet dann automatisch um, um den text “sichtbar” zu machen. Das ist bedeutend einfacher, als Texte innerhalb von \textit mit anderen Schriftarten/Schriftanweisungen von Hand hervorzuheben.

Bildnachweis: unsplash.com

LaTeX: Zitatangabe mit Seitennummer versehen

Um eine Seitennummer zu einer Zitatangabe in LaTeX hinzuzufügen, kannder Befehl “cite” mit einer Option verwendet werden. Zum Beispiel:

\cite[p. 111]{lipowski2017}

Das zitiert die Quelle und fügt die Seitenangabe “p. 111” in Klammern ein. Für deutsche Texte kann natürlich auch “S. 111” verwendet werden.

Eine weitere Möglichkeit ist die Verwendung des Pakets natbib und die Verwendung von \citep[p. 111]{author_year} oder \citet[p. 111]{author_year}.

Hier muss das Paket natbib importiert werden, damit diese Methode funktioniert.

\usepackage[numbers]{natbib}

Dazu muss sichergestellt sein, dass der richtige Zitierstil (z.B. plainnat) ausgewählt wurde.