9

I'm using XeLaTeX and fontawesome package.

This is my MWE:

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

\begin{document}

\section{\faGraduationCap \textbackslash faGraduationCap}
\section{\faBook \textbackslash faBook}

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

\end{document}

And this is the cropped result:

example

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.

6

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

  • 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
6

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.

  • 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
0

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

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

\section{Default \textbackslash fabox}

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

\faBox{GraduationCap} \textbackslash faGraduationCap\par\noindent
\faBox{Book} \textbackslash publications

\section{Left \textbackslash fabox}

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

\faBox[l]{GraduationCap} \textbackslash faGraduationCap\par\noindent
\faBox[l]{Book} \textbackslash publications

\section{Center (default) \textbackslash box}

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

\faBox[c]{GraduationCap} \textbackslash faGraduationCap\par\noindent
\faBox[c]{Book} \textbackslash publications

\section{Right \textbackslash box}

\subsection*{\faBox[r]{GraduationCap} \textbackslash faGraduationCap}
\subsection*{\faBox[r]{Book} \textbackslash faBook}

\faBox[r]{GraduationCap} \textbackslash faGraduationCap\par\noindent
\faBox[r]{Book} \textbackslash publications

\end{document}

And this is the result.

tobi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.