2

In my .tex document I use sans-serif font with \sffamily. (And I don't want this to be monospace!)

For my algpseudocode I tried using \texttt. (For I want using a serif monospace font.)

Please take a look at this minimal example

\documentclass[a4paper,10pt,english]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[a4paper,margin=2.5cm,footskip=0.5cm]{geometry}
\usepackage{algpseudocode}

\title{Example Document}

\begin{document}
\sffamily

\section{some content}
So, that's what my written content looks like. Sans-serif. Pretty nice, eh?

\texttt %When I remove this line, no error at all!

\begin{algorithmic}
\State x = 0
\While{x < 10}
  \State x = x+1
\EndWhile
\end{algorithmic}

\sffamily

But for some reason, this doesn't work.

\end{document}

I always keep those errors:

monospace-algorithm.tex:19: Paragraph ended before \text@command was complete. []
monospace-algorithm.tex:19: Too many }'s. []
  • What is \text@command?
  • I counted the } and the {. Their number seems correct.

  • Where do those come from?

  • How to make the pseudocode monospace? (I prefer to use standard commands like \sffamily instead of fonts.)
  • Unlike \sffamily, which is a switch and affects all text in the group after it, \texttt is a macro that usually only applies to its argument: \texttt{foo} bar. In the example \texttt does not have an argument and that might be problematic. You could go with the switch \ttfamily instead. But I'm quite sure there is a more elegant solution that does not require using all those font commands manually in the document. – moewe Sep 18 '18 at 5:41
  • thanx for \ttfamily. unfortunately, it breaks the pseudocode formatting. – Asqiir Sep 18 '18 at 5:47
  • 1
    You mean the bold? Yes, it is a well-known 'feature' of the default monospace font that it does not come with a bold series. You could try \usepackage{lmodern} where the bold is not very pronounced or look at tex.stackexchange.com/q/215482/35864 – moewe Sep 18 '18 at 5:51
1

algorithm contains a hook that you can redefine to set the font to a mono-spaced font.

In line with moewe's comment, you can use the lmodern package to get bold mono-spaced font.

Put this in your preamble:

% needed for bold tt font
\usepackage[lighttt]{lmodern}
% use sans serif by default
\renewcommand{\familydefault}{\sfdefault}
% use ttfamily for algorithm
\makeatletter
\algrenewcommand\ALG@beginalgorithmic{\ttfamily}
\makeatother

MWE

\documentclass[a4paper,10pt,english]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[a4paper,margin=2.5cm,footskip=0.5cm]{geometry}
\usepackage{algpseudocode}

% needed for bold tt font
\usepackage[lighttt]{lmodern}
% use sans serif by default
\renewcommand{\familydefault}{\sfdefault}
% use ttfamily for algorithm
\makeatletter
\algrenewcommand\ALG@beginalgorithmic{\ttfamily}
\makeatother

\title{Example Document}

\begin{document}

\section{some content}
So, that's what my written content looks like. Sans-serif. Pretty nice, eh?

\begin{algorithmic}
\State x = 0
\While{x < 10}
  \State x = x+1
\EndWhile
\end{algorithmic}

Now this works.

\end{document}

enter image description here

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.