Text FormattingMacro Fail

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Macro Fail

Post by LaTexLearner »

I think there is something very basic I am missing about macros.

I need to learn how to make answer spaces for my worksheets. So far I've used:

Code: Select all

\underline{\hspace{0.8cm}}
This works OK.

But, of course, I want all of my answer spaces to be uniform and I need to learn macros. I made this macro, but it didn't quite work.

Code: Select all

\newcommand{\answerspace}[1]{\underline{\hspace{#1}}
\answerspace{3cm}
The error said my argument definition was not good. What's going on here?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Re: Macro Fail

Post by Stefan Kottwitz »

You just forgot a closing brace } at the end of the macro definition.

Stefan
LaTeX.org admin
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Macro Fail

Post by Johannes_B »

Underlining some space works nicely, but seems a bit strange. How about using the simple and built-in rule?

Code: Select all

\documentclass{article}
\newlength{\answerlinethickness}
\setlength{\answerlinethickness}{.4pt}
\newlength{\defaultlinelength}
\setlength{\defaultlinelength}{1.5em}
\newcommand{\answerline}[1][\defaultlinelength]{\rule{#1}{\answerlinethickness}}
\begin{document}
And the asnwer is \answerline[2cm].

\begin{displaymath}
\begin{array}{r@{= \answerline}}
	3+4 \\
	7+1 \\ 
	47 + 7 \\
\end{array}
\end{displaymath}
\setlength{\answerlinethickness}{1pt}
And the asnwer is \answerline[2cm].
\end{document}
Here we use an optional argument to set the length of the line. The default value was set to 1.5em; 150% the width of the letter M.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Macro Fail

Post by LaTexLearner »

Stefan_K wrote:You just forgot a closing brace } at the end of the macro definition.

Stefan
Gah, ok. Thanks. The macro is working now. :)

Is that usually what that error message means when it says the definition is not good?
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Macro Fail

Post by LaTexLearner »

Johannes_B wrote:Underlining some space works nicely, but seems a bit strange. How about using the simple and built-in rule?
...
I'm trying to make sense of the \rule command. According to the LaTeX wiki the \rule command has 3 arguments. I get width and height. What is that optional one, depth?
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Macro Fail

Post by LaTexLearner »

I'm going to put my understanding of the code inside the code. Let's see if I actually understand it.
Johannes_B wrote:Underlining some space works nicely, but seems a bit strange. How about using the simple and built-in rule?

Code: Select all


\documentclass{article}

\newlength{\answerlinethickness}
\setlength{\answerlinethickness}{.4pt}
\newlength{\defaultlinelength}
\setlength{\defaultlinelength}{1.5em}

%These lines define the "ingredients" of the answer space macro below... but why did the first two have to \newLENGTH and \setLENGTH if they're actually setting THICKNESS?
%Those "ingredients" are how thick the answer line should be and how long they should be if not otherwise specified.
%This is also where I could adjust the default length and thickness of all answer lines in the whole document.



\newcommand{\answerline}[1][\defaultlinelength]{\rule{#1}{\answerlinethickness}}

%This is the actual answer line macro.
%These macros make it so that "\answerline" in the code will by default create a 1.5em line, but leaves the option open for \answerline[8cm] to create an 8cm line.
%There are two optional arguments here in the macro. 
%The first, [1], says how many optional arguments there can be in the code in the body.
%The 2nd, [\defaultlinelength], says that if the code in the body does not specify how long the answer line is, it should be whatever we set it to in the "ingredient" list above.


\begin{document}

And the answer is \answerline[2cm].

\begin{displaymath}
\begin{array}{r@{= \answerline}}
	3+4 \\
	7+1 \\ 
	47 + 7 \\
\end{array}
\end{displaymath}
\setlength{\answerlinethickness}{1pt}
And the asnwer is \answerline[2cm].
\end{document}
Is there any way I can make my comments in the code word wrap to make sideways scrolling unnecessary?
Last edited by LaTexLearner on Tue May 26, 2015 8:41 am, edited 1 time in total.
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Macro Fail

Post by LaTexLearner »

... Here we use an optional argument to set the length of the line. The default value was set to 1.5em; 150% the width of the letter M.
Does it have to be 1.5em? Could it also be 1.5ep or 1.53j? And would 1.5em change if the font changed the width of m? If so, wouldn't it be easier to set the default length, to, say, 0.6cm?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Macro Fail

Post by Stefan Kottwitz »

LaTexLearner wrote:Does it have to be 1.5em? Could it also be 1.5ep or 1.53j?
No. Designers usually consider the basic height of a lower case character and the width of a (wide) lowercase character as a property for referencing. x was chosen as height sample, m as width sample. Other characters usually scale proportionally. That means, if n would be 80% of the width of an m, the relation stays the same if you would resize the font. The width of m is the reference.
LaTexLearner wrote:And would 1.5em change if the font changed the width of m? If so, wouldn't it be easier to set the default length, to, say, 0.6cm?
Yes, em changes with the m glyph. But, easier or not, there's a purpose: if your drawing should scale with the font, choose em and ex, if it should stay the same even if you would change to a larger font, choose cm, pt, or any other absolute value. The relative values em and ex are very useful, since you don't need to adjust a lot of absolute values in case of a font size change.

Stefan
LaTeX.org admin
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Re: Macro Fail

Post by LaTexLearner »

I see... that makes a lot of sense.

So this means 1.5em will change not only if I switch the font *size* but if I switch from, say, Arial to Times New Roman (if the "m" changes width in those).
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Macro Fail

Post by Johannes_B »

LaTexLearner wrote:I'm trying to make sense of the \rule command. According to the LaTeX wiki the \rule command has 3 arguments. I get width and height. What is that optional one, depth?
As often, the LaTeX Wiki Book is strange, but who has the time to make changes?

The optional argument of the rule command is a raise, you can move the rule up or down using a length there. Best place to look up stuff like that is the unofficial LaTeX2e manual (texdoc latex2e)
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply