2

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}
4
  • There is a package for that --> \listings. read its manula by typing texdoc listings from terminal
    – user11232
    Nov 18, 2014 at 2:32
  • 2
    For just a word inline, though, \texttt{}? Whoever suggested you redefine \emph{} gave you extremely bad advice. Don't. Welcome to TeX SE, by the way!
    – cfr
    Nov 18, 2014 at 2:35
  • My suggestion would be to add \usepackage{listings} \lstsetup{basicstyle=\ttfamily} \let\comp\lstinline to your preamble.
    – Werner
    Nov 18, 2014 at 3:06
  • You might find this to be a fairly handy rundown on some of the ways to have LaTeX typeset code: sharelatex.com/learn/Code_listing
    – Ubiquitous
    Nov 18, 2014 at 10:44

1 Answer 1

3

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

2
  • I'm afraid the tutorial is not so excellent: the second example of \newcommand has a very bad error (missing % after the open brace). It may seem pedantry, but it isn't, as several questions on this site about this prove. The first example of \newenvironment goes even beyond this adding nefarious spaces on purpose! And the % that protect the end-of-lines in the examples of \newenvironment{simple} and \newenvironment{correct} are ridiculous (they are completely innocuous and useless).
    – egreg
    Nov 19, 2014 at 9:58
  • @egreg. Yes, that is rather sad. I hope that tutorial's author is also on this site and read your comment!
    – fajar
    Nov 22, 2014 at 2:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .