New answers tagged

0

\Make is defined to take two arguments, the first of which is optional, since it has the following structure: \newcommand{\Make}[2][]{% % <your definition> } As such, calling it with two mandatory arguments \Make{<a>}{<b>} will effectively ignore <b> as part of \Make, and assign #1 (the optional argument) an empty/blank value and #...


1

The way you've set up the macro \summ, taking 1 optional and 2 required arguments, you probably shouldn't write \summ{1}{2}{3}; instead, do give \summ[i]{1}{2} a try. You'll find out that it expands to \sum\limits_{i=1}^{2}. I think that defining \summ to take an optional argument is unnecessarily complicated. How about dropping the conditional and simply ...


1

In the case shown (the flag is defined) you were inserting \par in math mode which is not allowed. \documentclass{article} \usepackage{amsmath,color} \newbox\zzbox % I've got several environments for conditional rendering, here's one \newenvironment{filled}{\relax\ifdefined\filledflag\else\setbox\zzbox\vbox\fi\bgroup} {\ifdefined\filledflag\else\par\fi\...


7

The idea is good. Not so \ensuremath, of course. \documentclass[twocolumn]{article} % twocolumn is just to get a smaller picture \usepackage{amsmath} \ExplSyntaxOn \NewDocumentCommand{\ve}{m} { \mathchoice {\raymo_ve_display:n {#1}} {\raymo_ve_inline:n {#1}} {\raymo_ve_inline:n {#1}} {\raymo_ve_inline:n {#1}} } \cs_new_protected:Nn \...


6

Distinguishing inline and display math is different from distinguishing \displaystyle from \textstyle and can be tricky, since the align-like environments of amsmath use internally $\displaystyle...$. Your definition with \ensuremath will furthermore always use \textstyle if you call the macro outside of math mode. In my (of course questionable) opinion \...


2

LaTeX defines \author as \DeclareRobustCommand*\author[1]{\gdef\@author{#1}} You can define \school and \department in the same way: \DeclareRobustCommand*\School[1]{\gdef\@School{#1}} \DeclareRobustCommand*\Department[1]{\gdef\@Department{#1}} @ may not occur in the name of a macro on user level, so you have either to enclose the definitions between \...


1

A simple boolean option can be defined by package kvoptions: \documentclass{article} \usepackage{keyval} \usepackage{kvoptions} \SetupKeyvalOptions{ family=mosquito, prefix=mosquito@, } \DeclareBoolOption{hideInToC} \makeatletter \newcommand\sectionLvl[3][]{% \begingroup \setkeys{mosquito}{#1}% \ifcase#2\relax % 0 = chapter \def\tmp@...


2

Ad question 1: I think the classic way of handling the boolean would be using boolean keys as provided by packages like xkeyval or pgfkeys or by expl3. If you don't like to use such packages/interfaces, you can define your own mechanism for cranking out by means of delimited arguments whether the value "true/yes" or the value "false/no" ...


1

The following is using expl3 for the data processing. It works in the following way: First read in the first line of the file and parse the header. Find the column which is called summits, so that in further processing we know which column is the interesting one. Next read the file line by line and extract the $n$-th column (the one named summits). Split ...


5

What does LaTeX do when processing the definition of a command with an optional argument? Say we have \newcommand{\test}[2][default]{Optional: #1; mandatory: #2.} With a rather long sequence of steps that's not relevant for the discussion, the basic working is to do \def\test{\@protected@testopt\test\\test{default}} \expandafter\def\csname\string\test\...


4

The xpatch package can patch commands containing optional arguments. The following changes the definition without changing the optional argument's value (and can access the parameters using #1 or #2): \documentclass[]{article} \usepackage{xpatch} \newcommand\foo[2][default] {% Normal definition. Optional argument: #1. Mandatory argument: #2.% } \...


4

Let's look at \if..\else..\fi-conditionals: Expanding the \if... of an \if..\else..\fi while the \if..-condition is fulfilled yields evaluation and removal of the tokens that are needed for finding out whether the \if..-condition is fulfilled. E.g., expanding the \ifnum of \ifnum1=1 equal\else different\fi yields: equal \else different\fi . In further ...


3

A standard way to do this job is \documentclass{article} \makeatletter \newcommand\mySection[2]{% \ifnum #2=1 \expandafter\@firstoftwo \else \expandafter\@secondoftwo \fi {\section}{\section*}{#1}% } \makeatother \begin{document} test \mySection{title}{1} \mySection{title}{0} \end{document} so the conditional is completely removed before ...


4

You didn't count \expandafters well. Your test file should be: \documentclass{article} \newcommand\mySection[2]{ \ifnum #2=1 \expandafter\section \else\expandafter\section\expandafter*% \fi {#1} } \begin{document} test \mySection{title}{1} \mySection{title}{0} \end{document} The general rule: the \expandafters chain must touch ...


10

You should use \global\let as pointed out by David. Here's a shorter implementation with also an interface to print the problems. \documentclass{article} \ExplSyntaxOn \seq_new:N \g_digitalink_problem_seq \NewDocumentEnvironment{problem}{+b} { \seq_gput_right:Nn \g_digitalink_problem_seq { #1 } } {} \NewExpandableDocumentCommand{\getproblem}{m} { \...


10

It is never safe to place arbitrary LaTeX content in edef or xdef. The macro you want is already defined as \BODY so you need \let not \xdef to give it a global name, \documentclass{article} \usepackage{environ} \newcounter{problemnumber}\setcounter{problemnumber}{0} \NewEnviron{problem}{% \stepcounter{problemnumber}%%%%%% \global\expandafter\let\...


2

If you are an Emacs user, you can create an elisp function to generate the latex code for a matrix of your choice; (you can do the same with any algorithmic language, but in emacs Elisp is embedded) (defun matrice(type symb nrows ncolumns) "compose a LaTeX matrice type TYPE filling symbol SYMB number of rows NROWS number of columns NCOLUMS " (...


4

David Kastrup's \replicate-macros at http://www.gust.org.pl/projects/pearls/2005p/david-kastrup/bachotex2005-david-kastrup-pearl3.pdf might be of interest to you. In situations where you don't use the nicematrix-package with its \pAutoNiceMatrix you can define a variant of \replicate: \MyReplicate{⟨repetitions⟩}% {⟨tokens to ...


4

In OpTeX, we can define: \def\repmatrix#1x#2 #3{ \left(\vcenter{ \table{#2c}{\fornum 1..#1 \do {\fornum 1..#2-1 \do{#3&}#3\cr}}} \right) } $$ \repmatrix 2x3 {*} \not= \repmatrix 3x5 {*} \not= \repmatrix 2x2 {$Z$} $$ \bye


8

Without nicematrix. You may need to add \usepackage{xparse} if you're running LaTeX prior to the 2020-10-01 release. \documentclass{article} \usepackage{amsmath} \ExplSyntaxOn \NewDocumentCommand{\automatrix}{O{*}mmm} {% #1 = symbol (default *), #2 = delimiter, #3 = rows, #4 = columns \int_compare:nT { #4 > \value{MaxMatrixCols} } { \setcounter{...


14

with \pAutoNiceMatrix of nicematrix. \documentclass{article} \usepackage{nicematrix} \begin{document} $\pAutoNiceMatrix{7-7}{*}$ \end{document}


2

Define the command \heroap in a 'safe' way, i.e., use \NewDocumentCommand. Otherwise, when using the 'old-fashioned' way, you have to prefix \heroap in unsafe environments by \protect. Contemporary method: \documentclass{article} \usepackage{xstring} \newcommand{\hero}{John} \NewDocumentCommand\heroap{}{\IfEndWith{\hero}{s}{\hero'}{\hero's}} \begin{document} ...


1

New version of the luaotfload package (2020-12-31 luaotfload v3.16) introducess new virtual font features upper and lower. When they are used then the converting is done at font level, exactly as OP expects. For example, in OpTeX, we can try: \fontfam[lmfonts] \def\UppercaseIt{\setff{upper}\currvar} Normal text {\UppercaseIt a {\bf converted} text, ...


0

I am not sure this is going to reply to your question but you can use the package \usepackage{imakeidx} and \makeindex before your doc. For every word you wish to include, you would have to use the command \index{}after and finally \printindex at the end of your doc So, in practice \documentclass{article} \usepackage{imakeidx} %package \...


1

The problem is that the string -NoValue- that's used for testing is “strange”, so it's not really normal text. When you pass an \IfNoValueF test to description, it is essentially written “as is”. A solution is to declare that the relevant fields should be expanded. \documentclass{article} \usepackage[utf8]{inputenc} \usepackage[colorlinks]{hyperref} ...


7

You can use a .code type and invoke \pgfkeysalso inside the code instead of evaluating the conditional inside a .style. Internally a .style is just that, \tikzset{foo/.style={bar}} does the same as \tikzset{foo/.code=\pgfkeysalso{bar}}, but in .code you can evaluate arbitrary code, and not only call \pgfkeysalso. Note that you don't need append after command ...


0

Possible solution (has not failed my tests) and fits what I need :) \documentclass{article} \ExplSyntaxOn % Copy macros \cs_new_eq:NN \_enumerate_start: \enumerate \cs_new_eq:NN \_enumerate_stop: \endenumerate \NewCommandCopy{ \_item_std }{ \item } % Vars \int_new:N \g_enumerate_tempa_int \int_new:N \g_enumerate_tempb_int \int_new:N \g_enumerate_tempc_int \...


3

You need to remember that TeX works with tokens. and when grabbing an undelimited argument this will mean that we grab either One token or One balanced text: something inside braces* More formally, TeX absorbs everything after category code 1 token upto a matching category code 2 token, balancing any nested category code 1/2 pairs appropriately. Thus when ...


0

It is not only possible in LuaTeX. It is possible in plain TeX or LaTeX too. For example: \documentclass{article} \usepackage{amsmath, amssymb} \def\al#1\eal{\begin{align}#1\end{align}} \def\als#1\eals{\begin{align*}#1\end{align*}} \begin{document} \al \int_a^b \eal \als \frac{a}{b} \eals \end{document}


2

etoolbox provides \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>} that allows you to search for <search> within <cmd> and replace it with <replace>. It also allows for branching whether the replacement resulted in <success> or <failure>. I've used this approach below: \documentclass{...


3

The \def primitive was not menioned in other answers here. \def\command#1{\ifcase#1\or ABC\or CBA\or NNN\fi} \command1 % outputs ABC \command2 % outputs CBA \command3 % outputs NNN \bye


4

When having TeX create tokens by reading and tokenizing characters of a line of a .tex-input-file TeX will only consider characters of category code 11(letter) to be parts of names of control-sequences whose names consist of more than one letter. Digit-characters 0123456789 usually have category code 12(other). I.e., the sequence of characters (not tokens ...


5

And here an expl3 variant, which would work also with something different than numbers: \documentclass{article} \ExplSyntaxOn \newcommand\mycommand[1] { \str_case:nn {#1} { {1}{ABC} {2}{CBA} {3}{NNN} {x}{XXX} } } \ExplSyntaxOff \begin{document} \mycommand{1} \mycommand{2} \mycommand{3} \mycommand{x} \end{document}


8

If the choices are a consecutive range of numbers, I'd use an \ifcase construct: \newcommand\foo[1] {% \ifcase#1 Stuff for 0.% \or Stuff for 1.% \or Stuff for 2.% \else Stuff for other cases.% \fi }


6

Like this? \documentclass[11pt,a4paper]{report} \newcommand{\foo}[1]{% \ifnum #1=1 % ABC% \fi% \ifnum #1=2 % CBA% \fi% \ifnum #1=3 % NNN% \fi% } \begin{document} test \foo{1} test \foo{2} test \foo{3} \end{document} Of course, this approach returns nothing if you pass \foo{4}, for example.


1

I think that biblatex's \autocites has the most intuitive interface for the job you want your new macro to do. So I wouldn't look into creating a wrapper around \autocites, I would try to emulate \autocites for citation packages that do not support it. At this point I should probably warn you that I don't expect publishers to look favourably upon complex ...


3

But I never solved the problem of feeding an arbitrary number of sources to a command. If with "arbitrary number of sources" you mean "arbitrary amount of undelimited arguments", then one aspect of the answer can be the idea of tail-recursion. In TeX a so-called tail-recursive macro (or a tail-recursive macro mechanism) terminates by ...


2

I define the \Item command to just add the relevant contents to a sequence. Then, based on the optional argument or, if missing, the number of items, the appropriate number of items is selected by doing a mapping on the list. So, if the optional argument is [2,3], first the available width is divided by two and two items from the sequence are typeset in a ...


3

You can define \pmrow{1,2,3} and \pmcol{3,4,5,6} macro: \documentclass[12pt]{article} \usepackage{amsmath} \long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}} \def\pmrow#1{\def\tmpsep{&}\def\tmp{}\pmrowA #1,,} \def\pmcol#1{\def\tmpsep{\\}\def\tmp{}\pmrowA #1,,} \def\pmrowA#1,{\ifx,#1,\begin{pmatrix}\tmp\end{pmatrix}\else \ifx\tmp\...


3

This is for \pmtwo that works correctly putting the curly brackets. You have two numbers [2] i.e. {#1} and {#2} and \pmtwo not want the comma: hence $\pmtwo{1}{2}$. \documentclass[12pt]{article} \usepackage[utf8]{inputenc} \usepackage{mathtools} \usepackage{amssymb} %\newcommand{\pmone}[1]{\begin{pmatrix}#1 \end{pmatrix}} \newcommand{\pmtwo}[2]{\begin{...


1

The simplest way is to enclose what you want to pass to \autocites within braces in another command: \NewDocumentCommand{\myCitation}{m}{\autocites#1}. Therefore you can use \myCitation{{test1}[35]{test2}{test3}}. Another solution, which return only \autocite: \NewDocumentCommand{\MyCitation}{>{\SplitList{;}}m}{% \def\processCite##1{\foreach \k/\p in {...


2

No solution can do exactly the same as font switching, becuase font switching is done at TeX's main processor and next processing is in "normal" mode: all commands are interpreted just now. You can do only two pass processing, the parameter is scanned in the first pass and then it is put to \uppercase primitive. But the behavior differs, for ...


1

author is a name field, so you need to ask with \ifnameundef if it is given, not with \iffieldundef. Since author is not a field, but a name list, \iffieldundef{author} always gives true, so your code never gets to \printlist{organization}. You need to use \ifnameundef here. Since you print labelname, you probably shouldn't be testing for author, but for ...


2

Others already said why this isn't really possible, so I won't bring more sand to this beach. Here's an implementation that fakes that behaviour by scanning ahead until the next explicit close brace, then grabbing everything and passing it to \MakeUppercase as argument. The implementation uses expl3's new \peek_analysis_map_inline:n to read the input stream ...


1

This is not exactly what is asked for by the OP (a declaration). Rather it is a tokcycle pseudo-environment, which means that the declaration requires an explicit termination. That said, it handles spaces, works across paragraphs, manages macros automatically, as long as the arguments of those macros aren't affected by the case-changing method used here, ...


3

I suggest to use \doscheme macro wit syntax \doscheme[TMPA-TMPB-etc.]{nameA-nameB-etc.} which does: opens environment scheme does \schemeref[TMPA]{cmpd:nameA}\schemeref[TMPB]{cmpd:nameB} etc. in loop. inserts nameA-nameB-etc.eps picture. inserts capition Structure of \compound{cmpd:nameA} and \cmpound{cmpd:nameB} and ... (in loop) inserts label scheme:...


5

You can create a number of key-value options for \insertscheme: \documentclass{article} \usepackage{chemstyle,graphicx,xkeyval} \makeatletter \define@cmdkey{fam}{scheme}[\empty@key]{} \define@cmdkey{fam}{schemeopt}[]{} \define@cmdkey{fam}{compoundAref}[]{} \define@cmdkey{fam}{compoundAlabel}[\empty@key]{} \define@cmdkey{fam}{compoundBref}[]{} \define@...


6

If all your calls to \insertscheme are of pattern \insertscheme{⟨IDENTIFIER A⟩}% {⟨IDENTIFIER B⟩}% {⟨IDENTIFIER B⟩.eps}% {Structure of \compound{cmpd:⟨IDENTIFIER B⟩}.}% {⟨IDENTIFIER B⟩}% , then you can instead define \newcommand\...


13

If you are already using TikZ, you could as well use \pgfmathparse: \documentclass{article} \usepackage{tikz} \begin{document} \newcount\liczba \newcount\kwadrat \def\sqroot#1{\liczba=0\kwadrat=0 \loop \ifnum\kwadrat<#1 \advance\liczba by1 \kwadrat\liczba \multiply \kwadrat by \liczba \repeat$\sqrt{#1}\approx\the\liczba$} \newcommand\pgfsqrt[1]{\...


12

Here's a LuaLaTeX-based solution. It employs the Lua library functions math.sqrt and math.ceil. (The latter function returns the smallest integer that's no smaller than its argument; thus \math.ceil(11.0) returns 11, whereas math.ceil(11.01) returns 12.) \documentclass{article} \newcommand\sqroot[1]{\directlua{tex.sprint(math.sqrt(#1))}} \newcommand\...


Top 50 recent answers are included