Skip to main content
10 votes
Accepted

Is there a way to add optional and/or named argument to command

Classically the LaTeX2e solution was the keyval package (as used by \includegraphics[height=2cm, scale=3]{...}) but recent LaTeX releases include a keyval parser built in, based on l3keys. @Campa's ...
David Carlisle's user avatar
5 votes
Accepted

Redefine the command \cite to be able to use an additional syntax

I would not redefine \cite but if you want to, the desired argument specification may be directly declared with \NewDocumentCommand (see texdoc usrguide) \documentclass{article} \NewCommandCopy\...
David Carlisle's user avatar
5 votes
Accepted

"unexpected $ after $$" and "unclosed $$ found at $" errors with inline math inside display mode

These are not errors, but just wrong advice from Overleaf that is sometimes unable to comply with nested math formulas. It probably wouldn't balk if you write explicitly [\textnormal{$k$-alg}] as the ...
egreg's user avatar
  • 1.1m
4 votes

Is there a way to add optional and/or named argument to command

This might be a start using keyval \documentclass{article} \usepackage{keyval} \makeatletter \newcommand*{\task@definition}{} \newcommand*{\task@source}{} \newcommand*{\task@link}{} \define@key{task}{...
campa's user avatar
  • 31.7k
4 votes

Create a package with optional mode and key values

This is a rewrite of Heiko Oberdiek's answer using the new option processing facilities in recent versions of LaTeX. \NeedsTeXFormat{LaTeX2e}[2023-01-12] \ProvidesPackage{mypack}{2023/04/17 My package}...
cfr's user avatar
  • 205k
4 votes

Is there a way to add optional and/or named argument to command

For a simple task, imho1, nothing beats the simplicity of expkv-cs and \ekvcSplit (this only works for up to 9 keys, for more it's most likely better to use methods from expkv-def which work similar ...
Skillmon's user avatar
  • 61.3k
4 votes
Accepted

Custom proof environment: spacing issue with when optional argument given

Why even redefine the proof env when the one from amsthm works. In order to make it bold you can just use \usepackage{xpatch} \xpatchcmd\proof{\itshape}{\bfseries}{}{\typeout{Patch of proof failed}} ...
daleif's user avatar
  • 55.1k
3 votes
Accepted

Printing by matching argument with a string

Alice if the argument contains d any number of p and c, otherwise Robert \documentclass[a4paper,10pt]{article} \ExplSyntaxOn \NewDocumentCommand {\myCommand} {O{}} { \regex_match:nnTF {...
David Carlisle's user avatar
3 votes

Why do optional arguments of commands affect spacing of \mathcolor

The similar problem is in OpTeX, but not with regards to the next parameter given by an un-expandable macro. Simply $ {\Blue a^b}_c $ gives c after a^b (i.e. with space, wrong position) because the {...
wipet's user avatar
  • 75.5k
3 votes
Accepted

Why do optional arguments of commands affect spacing of \mathcolor

You can use \NewExpandableDocumentCommand that doesn't suffer from the problem of the check for the optional argument in the legacy \newcommand. \documentclass{article} \usepackage{xcolor} %MINIMAL ...
egreg's user avatar
  • 1.1m
3 votes

Environment with multiple optional arguments

You don't have to test for #4 as it's never empty it will always have a value, but you can not change that value from orange without also setting #3 as you have made the adjacent options #3 and #4 ...
David Carlisle's user avatar
3 votes

Redefine the command \cite to be able to use an additional syntax

I'm afraid that David's answer is not really correct. You need to check if an optional argument is passed, either as a “classical” one or after *. If not, then you need to call \oldcite without the ...
egreg's user avatar
  • 1.1m
2 votes

Is it possible to name macro (`\newcommand`) parameters and do math with them?

If you're doing this inside tikzpictures, you can just let pgfmath handle the calculations. Simply deleting the \newlength... and \setlength... lines works. \newcommand{\gogrid}[1][1]{% \draw[step=#...
cfr's user avatar
  • 205k
2 votes
Accepted

tcolorbox and ifmtarg: bad argument specification when checking for empty argument using @ifmtarg

The argument type O needs a default value: O{my default}, it's why you get an error. Use o if you don't need a default. In your case, since you check if the argument is empty, it's preferable to use O{...
jlab's user avatar
  • 3,759
2 votes

Autoref with optional argument

Updated solution for new LaTeX from TeXlive 2023/etc. onward: \AtBeginDocument{% \NewCommandCopy\autoreforig\autoref \RenewDocumentCommand{\autoref}{som}{% \IfBooleanF{#1}{% \...
yogik's user avatar
  • 95
2 votes

How to properly set optional boolean parameter for pic in tikz?

After some time I found this answer, that helped me a lot. So, we can use smth. like the following code: \documentclass{article} \usepackage{tikz} \usetikzlibrary{math} \newif\ifshowaxis \tikzset{ ...
Sindzicat's user avatar
  • 143
2 votes

Passing symbol to a command

What would key-value pairs look like? Note that, sensibly, the settings persist across incantations if not overwritten. MWE \documentclass{article} \usepackage{fontspec} \usepackage{xcolor} \...
Cicada's user avatar
  • 10.1k
2 votes
Accepted

Introducing colour ta a separator

You can't call \celest_separ:n [#1] nor \celest_separ:n [#1][#2] You need to absorb the arguments and call internal functions having the correct signature (and definition). But here I can't see how ...
egreg's user avatar
  • 1.1m
2 votes

Referring to arguments in newcommand not working in math mode

The code shown does not generate any error, as can be seen by \documentclass{article} \begin{document} \newcommand{\modspace}[4][0]{\overline{M}_{#1,#2,#3}(#4)} $\modspace{a}{b}{c}$ \end{document} ...
David Carlisle's user avatar
2 votes

Custom proof environment: spacing issue with when optional argument given

You could add \ignorespaces to your definition to ignore spaces at the beginning of the environment: \documentclass{article} \usepackage{amsthm} \renewenvironment{proof}[1][Proof]{\noindent{\bfseries ...
cabohah's user avatar
  • 13.4k
1 vote
Accepted

Is it possible to name macro (`\newcommand`) parameters and do math with them?

Everything will be working in your example, if you could define a suitable unit for the arg #1 in the \setlength command, while using the proper syntax for dimension calculation inside TikZ as well. ...
Tom's user avatar
  • 7,703
1 vote

Macro without arguments to define macro with arguments

You want ##1 in the replacement text. On the other hand I don't see why defining \mycounterHex that way, when it's not difficult to have an expandable version using any counter. \documentclass{article}...
egreg's user avatar
  • 1.1m
1 vote
Accepted

How to write a multiple argument environment within a newcommand?

You can add arguments to your \newenvironment, for example \newenvironment{#2s}[2][] defines the environment with one optional argument, which is by default empty, and one required argument. You ...
Andrew Swann's user avatar
  • 96.1k

Only top scored, non community-wiki answers of a minimum length are eligible