# Category: Coding & Scripting

## 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.

## Flutter: how to sort a list of objects by one of its properties

In Flutter (Dart), you can use the sort method on a list of objects, along with a custom comparator function, to sort the list by the alphabetical order of one of its properties.

Here’s an example of how you can sort a list of objects (let’s say they’re called Person) by their name property:

List<Person> people = [
Person(name: "Bob"),
Person(name: "Charlie"),
Person(name: "Alice"),
];

people.sort((a, b) => a.name.compareTo(b.name));

In this example, the sort method takes a comparator function as its argument. The comparator function compares two Person objects and returns a negative, zero, or positive value, depending on whether the first object should be sorted before, in the same position as, or after the second object. In this case, it compares the name property of each object using the compareTo method and sorts them alphabetically.

Another way to sort by one property is to use the sortBy method from the ‘dart:collection’ package:

import 'dart:collection';

people.sortBy((person) => person.name);

This will sort the list of people based on the name property.

## 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.

## mysqldump: how to exclude or include tables

By default, mysqldump includes all tables of the specified database when creating the dump. In some cases, it is useful to exclude some of the tables or even include only some of them. For me, this helped to exclude one of the biggest tables to reduce the backup execution time and file size.

## Exclude a single table

To exclude a single table, the argument --ignore-table followed by the table name is used:

mysqldump my_database --ignore-table=my_table > my_backup.sql

This will create a file my_backup.sql containing the sql schema and data of all tables in the database my_database except my_table.

## Exclude multiple tables

To exclude multiple tables, simply add the argument --ignore-table for all the tables you want to exclude:

mysqldump my_database --ignore-table=my_table --ignore-table=another_table > my_backup.sql

This will create a file my_backup.sql containing the sql schema and data of all tables in the database my_database except my_table and another_table.

## Include a single table

To include only a single table into the dump, add the name of the table to the command:

mysqldump my_database my_table > my_backup.sql

This will create a file my_backup.sql containing the sql schema and data of the table my_table from the database my_database.

## Include multiple tables

To include multiple tables into the dump, add the names of the tables to the command:

mysqldump my_database my_table another_table > my_backup.sql

This will create a file my_backup.sql containing the sql schema and data of the tables my_table and another_table from the database my_database.

Photo by benjamin lehman on Unsplash.

## Flutter: slowing down animation for debugging

The library that is responsible for different scheduling is flutter/scheduler. This also includes animations. To use the library, simply import package:flutter/scheduler.dart – in this case, timeDilation is all that you need.

Now you can set the variable timeDilation to a custom double. The value 1.0 is the default animation speed. Setting it to 2.0 will slow down all animations by a factor of two.

import 'package:flutter/scheduler.dart' show timeDilation;

// ...

timeDilation = 2.0; // Slow down animations by factor two

As timeDilation is a global variable, you can set it anywhere in the code, e.g. in main():

import 'package:flutter/scheduler.dart' show timeDilation;

void main() {
timeDilation = 3.0;
runApp(new MyApp());
}

Photo by LOGAN WEAVER | @LGNWVR on Unsplash

## JavaScript: simple code structure for libraries

The code of a JavaScript library might get very complex over time. This can be a problem for maintenance and expandability.

When writing a library, you should address two main points:

• Keep the environment clean from global variables and methods to avoid conflicts between different libraries
• Provide public and private methods and variables

The following “template” provides a simple code structure that fulfills those points:

// Unique name of the library.
MyLibrary = function() {
MyLibrary = {};

/*
* Internal variables and methods
*/
var variable = 'value';
function doSomething() {
// do something
}

/*
* Public variables and methods
*/
MyLibrary.myVariable = true;
MyLibrary.myAction = function() {
// do something
}

// Return the library.
return MyLibrary;
}

You can use such a library the following way:

const lib = MyLibrary();
lib.myAction()

if (lib.myVariable) {
}

Photo by Safar Safarov on Unsplash

## Flutter: red text and yellow lines in Text widget

When using a text widget, there are some configurations where the text turns red and gets yellow lines. In my case, it looks like in the following image.

The reason for this is a lack of style parameters from the parent widget. The red text shows that there is no color information available. The yellow lines show that text decoration is missing.

In my case, I have a simple text widget in a Row:

Container(
// ...
child: Row(
children: [
// ...
Text('External device'),
],
),
);

To solve the issue, there are different possibilities.

## Provide a DefaultTextStyle

DefaultTextStyle(
style: Theme.of(context).textTheme.bodyText1,
child: Text('External device'),
),

## Use Material

Material(
color: Colors.transparent,
child: Text('External device'),
),

## Use style parameter of Text

Text(
'External device',
style: TextStyle(
decoration: TextDecoration.none,
color: Colors.black,
),
),

In this case, decoration will remove the yellow lines and color will remove the red text color.

## Use Scaffold

A solution discussed in different threads might also be using a Scaffold for the parent widget. But this did not work in case.

Scaffold(
body: Container(
// ...
child: ...
);

## PHP: rounding a number and keeping the zeros after comma

In PHP you can use the round() method to round a double. This methods accepts a precision value as second parameter. Some examples:

echo round(3.1415926, 2); // "3.14"
echo round(3.1415926, 3); // "3.142"

When using round() on a value like 3.0000 the conversion to a string will result in just "3":

echo round(3.0000000, 2); // "3"

This is not wrong, but when you want to have a constant precision for different numbers, having an output of "3.00" is much more helpful.

To achieve this, you can use one of the following solutions.

## number_format()

echo number_format(3.1415926, 2); // "3.14"

## sprintf()

echo sprintf("%.2f", 3.1415926); // "3.14"