4

I'm trying to create a command to printout IP addresses on a LaTeX document with a different style. It's really simple what I want to do, but I would like to do it scalable.

I've already done this:

\newcommand{\ipAddress}{
    {\fontfamily{pcr}\selectfont
        {}
    }
}

And I was expecting to call the command on the text with the IP address as the argument, like this:

\ipAddress{100.64.0.0/10}

But it isn't working.

I'm really new to LaTeX and trying to give my best on the first "template less" document that I'm writing.

2
  • Welcome to TeX.SX! What does not working mean
    – user31729
    Commented Oct 12, 2015 at 19:47
  • The IP address 100.64.0.0/10 appears with the regular LaTeX font and not with the "pcr" font family. Commented Oct 12, 2015 at 19:48

2 Answers 2

8

Your command doesn't take an argument, so it effectively doesn't do anything. Try this:

\newcommand{\ipAddress}[1]{{\fontfamily{pcr}\selectfont #1}}

LaTeX provides a \texttt command which will set its argument in a monospaced ("teletype") font. So you can write \texttt{100.64.0.0/10}.

Even better, you can use the \let command to make a new macro that is semantically meaningful for you, such as \let\ipAddress\textt and then you can write \ipAddress{100.64.0.0/10}.

The \texttt command will render the text in whatever the default teletype family is. By default this is Computer Modern Typewriter. You can change the default by changing the \ttdefault command to another font family, using the abbreviations from the "New Font Selection Scheme". To set the teletype default to pcr, you would do this:

\renewcommand*\ttdefault{pcr}

So below is what I would do if I had to use pcr. You could modify this example to select any teletype font in combination with any other font packages.

\documentclass{article}

\renewcommand*\ttdefault{pcr}
\let\ipAddress\texttt

\begin{document}
The IP address is \ipAddress{100.64.0.0/10}.
\end{document}

Actually I prefer the Computer Modern Teletype, which is designed to harmonize with the rest of the family.

2
  • This worked flawlessly! Commented Oct 12, 2015 at 19:55
  • Maybe you want to mention \texttt.
    – egreg
    Commented Oct 12, 2015 at 20:07
3

Is this what you are looking for?

\newcommand{\ipAddress}[1]{%
    {\fontfamily{pcr}\selectfont%
        #1%
    }%
}

You do not mention which packages/engine you are using so this is my best bet as to what the problem is.

To understand the difference consider the expansion of your macro

\ipAddress{100.64.0.0/10} → {\fontfamily{pcr}\selectfont{}}{100.64.0.0/10}

as you can see the group where the font selection applies is closed before the ip is typeset.

By contrast, mine expands as

\ipAddress{100.64.0.0/10} → {\fontfamily{pcr}\selectfont 100.64.0.0/10}

which should do what you want.

In general, when you create a command with \newcommand{\macroname}[N]{DEF} in DEF you can use #1 to #N as placeholders for where you want the N arguments to be used.

As mentioned by @Andrew, if you use line breaks in DEF they get interpreted in the context of the macro expansion so they may mean 'there is a space here' which is not necessarily what you want. To neutralise them while keeping the lines separated in the code, you can make a comment right at the end of those lines.

5
  • 1
    You need [1] in the definition or LaTeX won't read the #1 argument, right? Commented Oct 12, 2015 at 19:54
  • This does not work: Illegal parameter number in definition of \ipAddress. Commented Oct 12, 2015 at 19:54
  • 1
    Perhaps you want % signs in the command definition to avoid extraneous spaces? Commented Oct 12, 2015 at 20:01
  • yes guys just pressed the wrong button too early =) now it's complete and should work!
    – Bordaigorl
    Commented Oct 12, 2015 at 20:01
  • @AndrewCashner good suggestion for %
    – Bordaigorl
    Commented Oct 12, 2015 at 20:02

You must log in to answer this question.

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