The absolute value is a math symbol that always gives the positive value of a number or expression. It is usually written between two vertical bars, like this: |...|
.
In LaTeX, it is important to learn how to write this symbol correctly. Doing so makes your equations look neat and professional.
Vertical bars from your keyboard
The simplest way to write an absolute value is to type two vertical bars (|) from your keyboard.
\[ |x| \]
Output :
This method works well for simple cases like |x|. However, for larger or more complex expressions, the vertical bars do not change size automatically.
Using the mid command
You can use the \mid
command to create vertical bars as well.
\mid x \mid
Output :
The \mid
command adds extra space around the expression. This extra space may not always look perfect
Perfecting absolute value with lvert and rvert commands
For a cleaner output, you can use \lvert
and \rvert
commands to make the vertical bars
\[ \lvert x \rvert \]
Output :
Let us compare three methods using a more complex example.
\[ |\frac{\partial x}{\partial t}| \] \[ \mid\frac{\partial x}{\partial t}\mid \] \[ \lvert \frac{\partial x}{\partial t} \rvert \]
Output :
In this output, the vertical bars look too small compared to the partial derivative symbol. This shows that using simple vertical bars, \mid, or \lvert…\rvert does not always work well for large expressions.
Automatic sizing with \left and \right
When you have larger expressions, you need vertical bars that adjust their size automatically. You can do this with the \left|
and \right|
commands.
\[ \left|\frac{\partial x}{\partial t}\right| \]
Output :
You can also use the \left
and \right
command before the \lvert
and \rvert
command to complete the same task that was used before the vertical bar.
\[ \left \lvert \sum_{i=1}^{n} x_{i} \right \rvert \]
Output :
This method ensures that the vertical bars change size automatically to match the height of the expression. It is the best practice for large or complex equations.
For matrices
If you need to show the absolute value of a matrix, you can use vertical bars around the matrix.
\usepackage{amsmath} .... \[ \left \lvert \begin{vmatrix} a & b\\ c & d \end{vmatrix} \right \rvert \]
Output :
This method displays the matrix inside vertical bars in a clean and professional way.
For piecewise functions
The value of |x| often depends on the range of x. Many times, the absolute value is used to show this. For example:
\[ \left | x \right |= \begin{cases} -x & \text{ if } x<0 \\ 0 & \text{ if } x=0 \\ x & \text{ if } x>0 \end{cases} \]
Output :
Conclusion
The best method to use is the \left
and \right
commands because they ensure that the absolute value bars adjust automatically to the size of the expression.
This method is especially useful for large equations and matrices.
Thanks for the tutorial mate