LaTeX frustration - defining commands with arguments

  • LaTeX
  • Thread starter Tickitata
  • Start date
  • Tags
    Latex
In summary, the conversation discusses the creation of a command in LaTeX for the legendre symbol, with one version for use in math mode and another for use in text mode. The attempt at a solution is to use the command \mlegendre{x}{y} which returns the symbol $x \overwithdelims () y$ in math mode, and (\frac{x}{y}) in text mode. However, there have been issues with the command producing odd results and causing errors.
  • #1
Tickitata
29
0
Hi all, I'm a beginner to typesetting using LaTeX. I'm not sure if this is the correct place to ask (the stickied LaTeX thread in Tutorials seemed to be devoted to TeX for the forums)

Homework Statement


I'm attempting to define a command \mlegendre{x}{y} which returns the legendre symbol [tex]$x \overwithdelims () y$[/tex] once already in math mode. My \legendre{x}{y}, to be used while in text mode, seems to be working just fine.

Homework Equations


N/A

The Attempt at a Solution


My attempt is
\newcommand{\mlegendre}[2]{#1 \overwithdelims () #2}

but this is producing very odd results in some places, and causing errors in others. For example,

$g_2 = \mlegendre{0}{7} $

produces [tex]$g_2 = 0 \overwithdelims () 7$[/tex]

/edit: okay well, apparently I don't know how to use PF's tex commands either, haha. In the first, it should have x in the top, and in the second, it should have parentheses around it.

Cheers!
 
Physics news on Phys.org
  • #2
Ah nevermind, I think I've got it with
\newcommand{\mlegendre}[2]{(\frac{#1}{#2})}
 
  • #3


Hello! I understand the frustration that comes with learning a new language or tool. LaTeX can be tricky at first, but with practice and patience, you will become more comfortable with it.

Regarding your attempt at defining the \mlegendre command, I would suggest using the \genfrac{}{}{}{}{}{} command instead of \overwithdelims. This will allow you to specify the size and style of the parentheses, making it more versatile for different situations.

Your command would look like this:

\newcommand{\mlegendre}[2]{\genfrac{(}{)}{}{}{#1}{#2}}

This will give you the desired output of $\mlegendre{x}{y}$ in math mode. Please note that this command will only work in math mode, so you may want to consider defining a separate command for text mode if needed.

I hope this helps and good luck with your typesetting!
 

FAQ: LaTeX frustration - defining commands with arguments

1. What is the purpose of defining commands with arguments in LaTeX?

Defining commands with arguments allows for greater flexibility and efficiency in creating documents. It allows you to create customized commands that can be reused throughout the document, reducing the need for repetitive code.

2. How do I define a command with arguments in LaTeX?

To define a command with arguments in LaTeX, you can use the \newcommand or \newcommand* command. The former allows for multiple arguments, while the latter only allows for one argument. You can then use the command by typing the defined name followed by the arguments in curly braces.

3. What is the difference between \newcommand and \newcommand* in LaTeX?

The main difference between \newcommand and \newcommand* is that the latter only allows for one argument, while the former allows for multiple arguments. Additionally, \newcommand* also prevents unwanted line breaks, making it useful for defining commands for short phrases or symbols.

4. How do I pass optional arguments to a command in LaTeX?

To pass optional arguments to a command in LaTeX, you can use the square brackets after the command name. These arguments will then appear in the defined command as #1, #2, and so on, depending on the number of optional arguments specified. You can also specify a default value for optional arguments by using the equals sign (=) after the argument name.

5. What are some common mistakes when defining commands with arguments in LaTeX?

Some common mistakes when defining commands with arguments in LaTeX include forgetting to use the curly braces around the arguments, forgetting to add a backslash before the command name, and using the same name for multiple commands. It is also important to remember to use the correct number of arguments in the defined command when using it in the document.

Similar threads

Replies
5
Views
1K
Replies
1
Views
3K
Replies
3
Views
700
Replies
15
Views
25K
Replies
4
Views
3K
Back
Top