# How to use fontawesome with fixed width?

I'm using XeLaTeX and fontawesome package.

This is my MWE:

\documentclass[]{article}
\RequirePackage{fontspec}
\RequirePackage{fontawesome}

\begin{document}

\section{\faBook \textbackslash faBook}

\faBook \textbackslash publications

\end{document}


And this is the cropped result:

How can I obtain a fixed width with fontawesome icons? I don't want to manually adjust the space each time and the solution should work both in simple text and inside sections.

Edit: If I could choose between ragged left, right or centered would be perfect.

With XeLaTeX or LuaLaTeX, a command such as \faBook gets translated into \faicon{book}, with a predefined table; so what we want to change is the action of \faicon, which by default is

\newcommand*{\faicon}[1]{{\csname faicon@#1\endcsname}}


Just change this into

\renewcommand*{\faicon}[1]{{\makebox[1.5em][c]{\csname faicon@#1\endcsname}}


I'm afraid you will have to adjust visually the fixed width depending on the widest icon you need to print.

\documentclass{article}
\usepackage{fontspec}
\usepackage{fontawesome}

\renewcommand*{\faicon}[1]{\makebox[1.5em][c]{\csname faicon@#1\endcsname}}

\begin{document}

\section{\faBook \textbackslash faBook}

\faBook \textbackslash publications

\end{document}


You can add an optional alignment argument to all fontawesome commands. The \ignorespaces makes the commands ignore a following space even if present, so

\faBook Publications
\faBook[r] Publications


are the same as

\faBook Publications
\faBook[r]Publications


and you don't need to worry about spaces if you add the optional argument.

\documentclass{article}
\usepackage{fontspec}
\usepackage{fontawesome}
\usepackage{xparse}

\RenewDocumentCommand{\faicon}{mO{c}}{%
\makebox[1.5em][#2]{\csname faicon@#1\endcsname}\ignorespaces
}

\begin{document}

\section{\faBook \textbackslash faBook}

\faBook \textbackslash publications

\noindent

\end{document}


• The result is the same of Tobi's answer but yours is cleaner and uses the builtin command faicon. However I improved the Tobi's solution in my answer to take the alignment as an optional argument. I tried to do the same thing with yours but without success. There is a way to add the alignment as optional argument using your solution (without redefining each icon)? – gvgramazio May 20 '18 at 11:10
• @gvgramazio Quite easy. ;-) – egreg May 20 '18 at 12:33

You could wrap the icons in a box of a fixed width. Furthermore I‘d make a new macro for this, to easily change the width later for example:

% !TeX program = xelatex
\documentclass{article}
\RequirePackage{fontspec}
\RequirePackage{fontawesome}

\newcommand{\faBox}[1]{%
\makebox[1.25em][l]{%
\csname fa#1\endcsname
}%
}

\begin{document}

\section{\faBook \textbackslash faBook}

\faBook \textbackslash publications

\section{\faBox{Book} \textbackslash faBook}

\faBox{Book} \textbackslash publications

\end{document}


The first argument of \makebox is the width, the second (optional) one the alignment in the box (center = default, left or right) and the last one is the content of the box, which actually could be wider than the box itself. In this case it‘ll overflow the box.

\csname and \endcsname can be used to “construct” a macro name an call it.

• I cannot edit you anwer because there are too few characters to edit: please change the brackets around 1em in the makebox call. – gvgramazio May 20 '18 at 10:52
• @gvgramazio: Sorry, it was late yesterday ;-) I fixed my code … – Tobi May 20 '18 at 12:46

I did an answer only because it has an additional feature. However, for an obviously reason, I will not mark my answer as an accepted one.

In the following an improvement of Tobi's answer. I added as optional argument the alignment inside the box.

\documentclass[]{article}
\RequirePackage{fontspec}
\RequirePackage{fontawesome}

\newcommand{\faBox}[2][c]{%
\makebox[1em][#1]{%
\csname fa#2\endcsname
}%
}

\begin{document}

\section{No \textbackslash fabox}

\subsection*{\faBook \textbackslash faBook}

\faBook \textbackslash publications

\section{Default \textbackslash fabox}

\subsection*{\faBox{Book} \textbackslash faBook}

\faBox{Book} \textbackslash publications

\section{Left \textbackslash fabox}

\subsection*{\faBox[l]{Book} \textbackslash faBook}

\faBox[l]{Book} \textbackslash publications

\section{Center (default) \textbackslash box}

\subsection*{\faBox[c]{Book} \textbackslash faBook}

\faBox[c]{Book} \textbackslash publications

\section{Right \textbackslash box}