[Tex/LaTex] Create a command for a different font

emphasismacros

I'm pretty new to Latex and I am writing my thesis. I would like to use a command that I can essentially change the font of a few words in-line or separate (like an equation).

I have computer commands that I would like to display in a different font (Courier New) than the rest of the document. I have heard that I could either modify the \emph{} command or create a new command like \comp{} that changes the font.

I don't know how to do either. Is there a package that already does this for me? Thanks for your help

Example:

Next you need to position the robot arm by using the command \comp{Home}.

OR

\comp{cd folder ./trajectory.exe}

Best Answer

What you want is a macro. Wikibooks has excellent tutorial on the topic. As for the code environment, use the listings package: Wikibooks, CTAN.

Here is a quick example of what you can do. The \newcommands there make three, well, new commands: \compbox, \compline, and \comp.

\documentclass[a4paper]{article}
\usepackage{fancybox}
\usepackage{listings}
\lstset{%
language=Python,%
basicstyle=\renewcommand{\baselinestretch}{1}\ttfamily,%
numbers = left,
numberstyle = \tiny,
numbersep = 5pt,
} %% Many other options!

\newcommand{\compbox}[1]{\ovalbox{\texttt{#1}}}
\newcommand{\compline}[1]{\underline{\textbf{\texttt{#1}}}}
\newcommand{\comp}[1]{\textbf{\texttt{#1}}}

\begin{document}

Hit the \compbox{F5} key. Next you need to position the
robot arm by using the command \compline{Home}. Now run
this on the terminal:

 \comp{texdoc listings}

Which should open \comp{listings} documentation.
Then you can write something like this:

\begin{lstlisting}
import math
import sys
def cube(x):
    return x*x*x
# An example of Python code
# in LaTeX
\end{lstlisting}

\end{document} 

Output

Related Question