We may need backslash and (forward)slash characters while creating documents.
Backslash character is used at the beginning of any command, meaning this character indicates a LaTeX command, so this character cannot be printed directly in LaTeX.
And it is possible to print the forward slash character directly but it may cause some problems.
In this tutorial, we will discuss all methods of printing backslash and slash characters in LaTeX documents.
Backslash(\) in LaTeX
Because the backslash indicates a LaTeX command, you cannot print this character directly into a document.
There are several commands that you can use to print this character in a document, some of which work in text mode and some in math mode.
Backslash in text mode
LaTeX provides a default command to print this character in text mode, which is \textbackslash
.
You can also print this character in a LaTeX document using the \symbol{92}
command from the \usepackage[T1]{fontenc}
package and, this command works in both text mode and math mode of LaTeX.
\documentclass{article}
\usepackage{amssymb} % for \longrightarrow
\usepackage[T1]{fontenc} % for \symbol{92}
\begin{document}
\verb|a\symbol{92} b|\longrightarrow a\symbol{92}b \\[4pt]
\verb|a\textbackslash b|\longrightarrow a\textbackslash b
\end{document}
Output :
Backslash in math mode
There are two default commands for using the backslash character in LaTeX’s math mode, \backslash
and \setminus
.
Besides, the amssymb package provides two commands which are \smallsetminus
and \diagdown
. The following table lists the commands along with their outputs.
Package | command and output |
---|---|
amssymb | \smallsetminus → |
amssymb | \diagdown → |
None | \backslash → |
None | \setminus → |
LaTeX provides a command called \verb|..|
. The function of this command is that if you write any command or character in it |..|
, it will not compile and print it directly to the document.
But the font will be slightly different in the output. You can also use this to print the backslash character in the document.
\documentclass{article}
\usepackage{amssymb} % for \longrightarrow
\usepackage[T1]{fontenc} % for \symbol{92}, \smallsetminus, \diagdown
\begin{document}
\[ \verb|a\backslash b|\longrightarrow a\backslash b \]
\[ \verb|a\setminus b|\longrightarrow a\setminus b \]
\[ \verb|a\symbol{92} b|\longrightarrow a\symbol{92}b \]
\[ \verb|a\smallsetminus b|\longrightarrow a\smallsetminus b \]
\[ \verb|a\diagdown b|\longrightarrow a\diagdown b \]
\end{document}
Output :
Forward slash(/) in LaTeX
You can print the slash character directly to the document, but the problem here is that if you write it like this village/city
, it will never split even though they are two separate words.
In other words, if at the end of a line, there is a space like a word between these two words, then the whole village/city
will move to the next line rather than split or stay on the same line, which can spoil the look of the paragraph.
To overcome this problem LaTeX provides a default command which is \slash
.
With this command you can print this character in a LaTeX document and there will be no word splitting problem. And you can use it in both math mode and text mode.
\documentclass{article}
\begin{document}
You can achieve anything. It doesn't matter if you are from a small city/village. You can definitely do it.
\par
You can definitely do it. It doesn't matter if you are from a small city\slash village.
\end{document}
Output :
There are also two other commands that let you print the slash character to the document.
They are \textfractionsolidus
command from textcomp package that you can use in both text mode and math mode, and the \diagup
command from the amssymb package that only works in math mode.
\documentclass{article}
\usepackage{amssymb}
\usepackage{textcomp}
\begin{document}
\[ \verb|a/b|\longrightarrow a/b \]
\[ \verb|a\slash b|\longrightarrow a\slash b \]
\[ \verb|a\textfractionsolidus b|\longrightarrow a\textfractionsolidus b \]
\[ \verb|a\diagup b|\longrightarrow a\diagup b \]
\end{document}
Output :