1

I'm using Overleaf.

I created a simple command to combine \textbf and \texttt.

\newcommand{\textbftt}[1]{\textbf{\texttt{#1}}}

The definition is in the document prelude file.

I use the \textbftt command elsewhere in the document, e.g., textbftt{return}. It seems to work fine.

However, when I edit the file where \textbftt is used (and the edit has nothing to do with the use of \textbftt) the command no longer works. The text is rendered with neither bf nor tt.

I also see an error marking in the margin (a small red box with an 'x'). The attached error message says that the command is used in a way that is inconsistent with its definition. (How can that be if it worked properly at first?)

More confusing is that if I then make an extraneous edit to the file containing the definition, e.g., add an extra space someplace in the file, the command now works properly--until I again edit the file where it is used. (The same thing happens if I use \def instead of \newcommand.)

I find myself making repeated meaningless changes to the prelude file, e.g., adding and removing an extraneous space) to see what my document looks like.

Any thoughts about what might be going on?

Are the changes to the prelude file forcing Overleaf to re-read that file--and hence the definition? Doesn't it always do that when it recompiles?

Thanks.

  • 2
    Welcome to TeX.SX! Your command should work without problems and it shouldn't trigger a Use of \textbftt doesn't match its definition. Can you please show a compilable example that reproduces the issue? – Phelype Oleinik Jun 18 at 22:14
  • Can you see this Overleaf project: overleaf.com/3972574292hrzwtqtkysqq. (I'm not the owner, although I can ask the owner to share it.) The definition is in main.tex; the use is in body.tex (and elsewhere). – RussAbbott Jun 18 at 22:23
  • The definition in the project you linked to says \def\textbftt[#1]{\textbf{\texttt{#1}}}, and not what you show in the question. With the above definition you must use the command with \textbftt[text] instead of \textbftt{text}. Change the definition to what you show in the question and it should work. – Phelype Oleinik Jun 18 at 22:26
  • 1
    Rather than posting links to an external site, which may disappear in he future rendering your question meaningless, please give a minimal working example. This should compile and be the smallest amount of code necessary to demonstrate your problem. – Andrew Jun 18 at 22:26
  • @RussAbbott The remaining errors I see are due to the usage of \item outside a itemize or enumerate or another list environment and due to a missing definition of \pyth... – Phelype Oleinik Jun 18 at 22:28
6

In your document you have/had:

\def\textbftt[#1]{\textbf{\texttt{#1}}}

and in the question you said you had:

\newcommand{\textbftt}[1]{\textbf{\texttt{#1}}}

First things first: unless you really, absolutely know what you're doing, you should use \newcommand instead of \def in a document. If you look carefully, the LaTeX manual doesn't even explain the usage of \def, so it (sort of) doesn't belong to the system (of course it works because LaTeX runs on top of TeX, but it really shouldn't be used in an everyday document). \def is for "experts"!

Now, both of the definitions above are correct and both will work if used properly (what doesn't :-). However both define two very different commands!

\newcommand

Let's first tackle the second one, which is "easier". The syntax for \newcommand is:

\newcommand{<command>}[<num args>][<default>]{<replacement text>}

where both [<num args>] and [<default>] are optional, but the former is mandatory if the latter is to be used. <command> is the command name, in your case \textbftt, which you defined to take <num args>=1 argument, with no optional arguments (because <default> is not given) and the <replacement text> of this command is \textbf{\texttt{#1}}, where #1 is the first (and only) argument to the command, enclosed in braces ({...}). This all means that when you use that definition:

\textbftt{<some text>}

TeX will expand \textbftt, replacing the #1 in the <replacement text> by the stuff between (balanced!) braces:

\textbf{\texttt{<some text>}}

which is what you want.

\def

Now the \def version is trickier. The syntax of \def is (simplifying a little):

\def<command><parameter text>{<replacement text>}

where <command> and <replacement text> are the same as the ones in the \newcommand version. The <parameter text> is what tricked you because, well, it's the tricky part :-)

The <parameter text> can be arbitrary text, with the exception of { and }, which can contain up to 9 # symbols followed by consecutive numbers from 1 to 9 (i.e., #1, #2, ... #9---these are the "parameters"). Whatever you use in the <parameter text> except for the parameters must match exactly when you use the <command>. In your definition \def\textbftt[#1]{\textbf{\texttt{#1}}}, the <parameter text> is [#1], so whenever you use \textbftt TeX expects to find exactly a [, followed by arbitrary (balanced---balanced pairs of {...}), possibly empty, text, until the next ]. If TeX doesn't find any of these it will say that the Use of \textbftt doesn't match its definition.

Now, knowing that you need to enclose the text in [...] instead of {...} you now can use the command like in the previous version:

\textbftt[<some text>]

then TeX will expand \textbftt:

\textbf{\texttt{<some text>}}

and you have the same result, but with a different definition and usage.

The catch

But now you say: "Hey, I can use \textbftt[<some text>] instead of \textbftt{<some text>}!"

Well, yes, you can. But you should not. The LaTeX manual defines that optional arguments should be enclosed in [...], not mandatory arguments. Going against this convention makes the usage of the command counter intuitive and potentially problematic.

The other problem is that TeX knows that {...} are "delimiters", while [ and ] are just two characters just like ! and , and @ and... This means that if you need to use other [ or ] you need special protection to ensure that TeX does the right thing. For example:

\textbftt[some [text] with square brackets]

will not do what you want.
It will expand to:

\textbf{\texttt{some [text}} with square brackets]

And, finally, of course, \newcommand checks whether the command you are trying to create already exists and throws an error if you are about to redefine something important. \def just does it and you will only notice when chaos ensues.

If you search this site you'll see tons of examples of "you should've used \newcommand instead", so I suggest you stick to it unless you are sure of what you're doing.

  • Thanks for all the info. I'm more than happy to use \newcommand. The only reason I tried \def is that \newcommand wasn't working consistently for me. – RussAbbott Jun 19 at 0:07
  • As I said above, I'm more and more confused. My file has the \def version commented out. (It did when I posted my question.) I don't know why you saw it as the operative definition. – RussAbbott Jun 19 at 0:09
  • Also as I said above, when I followed the link I posted the problem didn't occur. I have no idea why. But I was happily editing away until I defined another \newcommand. Then the same thing happened. Both commands stopped working until I added an extraneous space to main.tex. This time, however, the error message says undefined control sequence instead of use inconsistent with the definition. I'm feeling defeated by Overleaf/LaTeX. – RussAbbott Jun 19 at 0:17
  • 1
    @RussAbbott I looked at your project again, and the problem is Undefined control sequence \textttbf. The command you define is \textbftt (the tt and bf are swapped). If you click the error message Overleaf takes you to where it happened. – Phelype Oleinik Jun 19 at 0:20
  • A colleague noticed that the project main menu said that the main document is main_bak.tex. At one point I tried to simplify main.tex and saved the old one and created a new one. I guess I saved the old one by renaming it from main.tex to main.bak.tex. Then I probably created a new main.tex. Overleaf must have changed the project settings to keep the old main.tex (now renamed to main_bak.tex) as the main document. When we changed the settings so that main.tex was the main document as intended, most of the problems cleared up! – RussAbbott Jun 19 at 5:29

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.