How to use fontawesome with fixed width?

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{\faGraduationCap \textbackslash faGraduationCap}
\section{\faBook \textbackslash faBook}

\faGraduationCap \textbackslash faGraduationCap\par\noindent
\faBook \textbackslash publications

\end{document}

enter image description here

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{\faGraduationCap \textbackslash faGraduationCap}
\section{\faBook \textbackslash faBook}

\faGraduationCap \textbackslash faGraduationCap\par\noindent
\faBook \textbackslash publications

\noindent
\vrule\faGraduationCap\vrule\faBook\vrule \\
\vrule\faGraduationCap[l]\vrule\faBook[l]\vrule \\
\vrule\faGraduationCap[r]\vrule\faBook[r]\vrule

\end{document}

enter image description here


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{\faGraduationCap \textbackslash faGraduationCap}
\section{\faBook \textbackslash faBook}

\faGraduationCap \textbackslash faGraduationCap\par\noindent
\faBook \textbackslash publications

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

\faBox{GraduationCap} \textbackslash faGraduationCap\par\noindent
\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.