How do I write a function in LaTeX? I tried writing f(…), but it just appears as a normal f. Do I have to add a math package or is it already in LaTeX

# [Tex/LaTex] Writing functions and LaTeX

math-mode

#### Related Solutions

Most of what I say is covered by other answers, but I will write here my thoughts.

First of all, if you write a lot in LaTeX format, I think you should leave LyX and go for any plain text editor. In the (near) future you will be faster (reading and writing code).

About writing readable code. Here are some tips to make it readable:

- One of the main features I found in LaTeX is the ability you have to write what you want. But don't fail at deciding what you want! I mean, in your code you (shouldn't want) don't want
`x`

to be bold (`mathbf`

), you must want`x`

to be a vector (`\vec{x}`

). You will decide later what a vector looks like, but at first, you don't have to worry about it. - Next, if you usually define some sets you can define
`\set`

command (and later you will worry about how sets are displayed), but, as David Carlisle pointed, you can define shorter commands. - This is a must have in my documents:
`\R`

(`\mathbb{R}`

) to define the real numbers. It is an entity so I define a command to call them. But I wouldn't define`\Rn`

for`\mathbb{R}^n`

, because then you aren't writing what you want, you are fastening your code input (which shouldn't be done that way). - May be
`\quad`

is not easy readable for you, but I think it will in the future. - This example is no so long, so you can't see how indenting your code benefits the reading of your code, but it really does.
- I use
`\[`

,`\]`

but I don't use`\(`

and`\)`

because I don't read math really good between those delimiters. So I changed them to`$`

. - Personally, I don't write comments in my code unless I need to edit it later or something (I use comments as reminders). If you read code fast you don't need comments which explains what are you doing (in my case, they break my concentration).

This is how I would write your code:

```
\documentclass{article}
\usepackage{mathtools}
\usepackage{amsfonts}
\renewcommand\vec[1]{\mathbf{#1}}
\newcommand\set[1]{\mathcal{#1}}
\newcommand\R{\mathbb{R}}
\begin{document}
\subsection{Convex sets}
A set $\set{S}$ in $\R^n$ is said to be convex if
\[
\vec{x}_1, \vec{x}_2 \in \set{S} \implies \lambda \vec{x}_1 + (1-\lambda) \vec{x}_2 \in \set{S} \quad \text{for all } 0 < \lambda < 1
\]
\end{document}
```

After that, I think you should differentiate writing code and fastening your input. Most of people defines commands to write faster (like `\newcommand\vx{\vec x}`

from Niel de Beaudrap). I don't agree with that. To write faster you should use an editor (or a third party app) which lets you create snippets (i.e. I would make a snippet with `\vec{x}`

which would be called when you type `vx`

).

At last, to fasten your code-reading you must use a program which shows your code with colors (a color for math, a color for commands, a color for text,…). This is an example of my code (the theme and font I show here is not the usual, but I'm not in my main operating system, however I don't hate this setup):

## Summary

These are my ideas:

- Write what you want (but don't misunderstand what you really want). The commands you use have to be
*entities*, not just 'shortcuts'. Later, you will care about how will it look like. - Don't use commands as snippets (use a good editor or a third party app to make use of snippets).
- Indent your code.
- Use an editor which displays the code with different colors.
- Again, if you want your code to be readable don't use
`\mathbf{x} \in \mathbb{R}^n`

or`\vx \in \Rn`

. I would go for`\vec{x} \in \R^n`

.

In the case of `\max`

, the text below automatically. For other operators (like `\int`

) or for inline formulas, you can use `\limits`

. Longer text has to be in curly brackets:

```
\max\limits_{n_j \in succ(n_i)}
```

To get a line over your text, you can use `\overline`

:

```
\overline{w_i}
```

Your formulas would be:

```
rank_u(n_i) = \overline{w_i} + \max\limits_{n_j \in succ(n_i)} (\overline{c_{ij}} + rank_u(n_j))
rank_d(n_i) = \max\limits_{n_j \in pred(n_i)} \{rank_d(n_j) + \overline{w_j} + \overline{c_{i,j}}\}
```

If you want to typeset *rank* and *succ* as an operator like max, you can either use `\text{rank}`

from `amsmath`

, or declare *rank* as a math operator (in your preamble):

```
\DeclareMathOperator{\rank}{rank}
\DeclareMathOperator{\successor}{succ}
```

Please note that I chose `\successor`

over `\succ`

, as this is already used for a math symbol.

## Best Answer

Anything that is maths should go in math mode. There are a number of ways of entering math mode, and actually different types. The simplest is

`$ ... $`

Thus, you will write

`$f(...)$`

This is for in-line maths, maths that should go in the text, e.g.:

You may also use:

`\( ... \)`

Which does exactly the same thing:

For displayed equations, you should use:

`\[ ... \]`

This will display your equation. By default it will be on a new line, centred, and there will be a small skip between the lines of text preceding and succeeding the equation. In addition, certain symbols - such as the summation and integral signs - will be set larger by default. Fractions will also be larger by default and limits, etc. will, by default, be placed above and below (except with integrals). This is suited to large displayed equations. In inline math mode we have smaller symbols, limits are set like sub- and superscripts and fractions appear smaller. This is so that the mathematics is integrated smoothly into the text and does not cause ugly line heights and excessive white space.

So you could have:

There are also a number of environments, such as

`equation`

and`flalign*`

which are maths environments and put you, by default, into math mode, e.g.:These environments usually display your equations.

P.S.: You should definitely load the

`amsmath`

package at the least.