2

I want to describe C-functions in a document, e.g.:

The function fooBar() does foo and bar...

Should I use a different typeset for the function in the text? I have the feeling that for example \textsc{fooBar()} highlights the function in the text, but for a high cost in appearance.

  • 1
    You can use listings package. – user11232 Jun 9 '14 at 1:39
  • 1
    If you just want some unique formatting for code, I personally would use \texttt{fooBar()}, which uses a monospaced font. – ChrisS Jun 9 '14 at 1:55
  • 3
    Close voters, if anything this is a duplicate rather than off topic. Writing source code in LaTeX as text See also Package for displaying source code – Alan Munn Jun 9 '14 at 5:00
  • 1
    As a combination of the comments of H. Kumar and ChriS: You can use \lstinline{fooBar()} to typeset it as some piece of code appearing somewhere in your text, e.g. even with logical markup. – user31729 Jun 9 '14 at 5:34
  • 1
    @AlanMunn: I agree with you, if closed, it should be done so for the correct reason: being a duplicate, but not off-topic, in my point of view. – user31729 Jun 9 '14 at 6:37
1

You can achieve your goal in different ways. One is to use teletype family fonts \texttt{foobar()} as chrisS has mentioned in the comments. These are fixed-width fonts. Another option, is to use \emph{foobar()} style. Although in normal text '\emph{} seems to be italic, but it is actually more than that. For example, if your text is italic and you use \emph{foobar()} for your function, latex makes it visually distinct by making it normal text. In short, \emph{} guarantees a visual distinction but it is not necessary always pleasing.

enter image description here

Image from The Not So Short Introduction to LaTeX2ε.

My suggestion would be to define a macro of your own such as

\newcommand{\MyCode}[1]{%
\texttt{#1}}

And you it like The function \MyCode{fooBar()} does foo and bar...

This one has the advantage that in case you decide texttt is not what you want, you can change only your macro definition and the effect will apply to your whole document.

For longer pieces of the code, there are packages like listings that can you you with typesetting of different languages (e.g. this Q/A).

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.