tags:

views:

515

answers:

5

I have been using LaTeX from sometime now, but have never actually gotten my hands dirty declaring a new command, as I try to avoid that.

However, I need to add monospace text often in my document and I use \verb for it which is fine, except that the verb font size is bigger than the normal text font. So I need to change the font size and undo done like \small{}\verb#My monospace code#\normalsize{}. This is not very convenient and mistake-prone.

Is there a better way to do this? Can I define a new command for this? How?

A: 

Let me give some examples of new command:

\newcommand{\zope}{Zope}

Now whereever I give \zope in my Latex text, its replaced by the text specified above. This if I need to make a global change I can make it.

\newcommand{\bi}{\begin{itemize}}
\newcommand{\ei}{\end{itemize}}

With the help of the two commands above, now I can write my lists as:

\bi
\item 1st item
\item 2nd item 
\item and so on...
\ei

This illustrates how some commands can be shortened with the help of newcommand.

Now lets consider a newcommand with a parameter.

\newcommand{\filepath}[1]{\verb!#1!}

To use this command, I would write something like \filepath{abc.xml} which will be replaced by \verb!abc.xml! on processing the command.

Another example:

\newcommand{\ida}[1]{\textcolor{blue}{#1}}

With help of this \ida{myVariable} is replaced with \textcolor{blue}{myVariable}

I hope this small tutorial will help serve the purpose.

Shailesh Kumar
Nice examples but, critically, the one with \verb doesn't work.
Will Robertson
Let me amend that by saying that it won't work to escape special characters in the input like \verb is supposed to do.
Will Robertson
+2  A: 

Why not use the \texttt{} command to get monospaced text?

I'm not sure why the \verb command is giving you text that's larger than normal (for me, \verb is exactly the same size as regular text, and \texttt{} as well). However, if you really want to make a new command to get you smaller monospaced text, you might try defining a command in your header as follows:

\newcommand{\smalltt}[1]{{\small\texttt{#1}}}

Where the [1] indicates that the command takes one argument (the text to make small and monospaced.) Then of course you'd use it as \smalltt{my smaller monospaced text.}.

Noah
+1  A: 

As Noah says, the size should not be different between \verb text and normal output. It would be a good idea to post a small, complete example of the form:

\documentclass{article}
\begin{document}
Text and \verb|verbatim % text|.
\end{document}

If you want to pick up text to process verbatim with formatting, things are a bit complicated by the need to watch catcodes. I'd look at the fancyvrb or listings packages for this.

Joseph Wright
+1  A: 

Defining new commands that act like the verbatim commands is actually quite difficult. The LaTeX verbatim package contains some stuff that will help.

If you are using the times package, the Computer Modern verbatim font does not go well with the PostScript times fonts. For a more suitable verbatim (typewriter) font try

\renewcommand{\ttdefault}{aett}
Norman Ramsey
txtt is also a good match for Times (and others).
Will Robertson
Thank a lot. :-D
NawaMan
A: 

Put the following line in a .sty file included by your document: \renewcommand{\verbatim@font}{\ttfamily\small}

Steven Bird