Tag Info

New answers tagged

3

You are leaving end of line spaces. Put % at the end at two places like \newcommand\Myhref[2]{%<-------- here \href{#1}{#2}% <-------- and here } Then you are good to go. \documentclass{article} \usepackage{hyperref} \newcommand\Myhref[2]{%<-------- here \href{#1}{#2}% %<-------- and here } \begin{document} A link is placed ...


1

You could use \SetKwBlock: \documentclass{article} \usepackage{algorithm2e} \SetKwBlock{Repeat}{repeat}{} \begin{document} \begin{algorithm} \Repeat{do these things} \While{$i<n$}{a while loop\; \Repeat{do other things} this condition\; } \end{algorithm} \end{document}


1

Usually this is realized in the form \foo{Unique} and \foo[One]{Two} which is clearer than an optional argument in braces. The classical LaTeX way to do this is \newcommand{\foo}{\@dblarg\name@foo} \def\name@foo[#1]#2{Whatever we want to do with #1 and #2} So calling \foo{X} will result in Whatever we want to do with X and X whereas calling ...


3

Here's a xparse solution using the g specifier as a possible optional 2nd (!!!) argument, the g specifier allows for {} delimited optional arguments, but in my point of view, [] would be a clearer way. \documentclass{article} \usepackage{xparse} \NewDocumentCommand{\xparsecmd}{mg}{% \IfValueTF{#2}{% optional #1 and #2 }{% Only #1 and #1 }% ...


3

This syntax relies on the fact that one will use braces to enclose arguments (otherwise the question is ambiguous since any subsequent token can be interpreted as a second argument). \documentclass{article} \makeatletter \def\mycommand#1{\@ifnextchar\bgroup{\mycommandhelp{#1}}{\mycommandhelp{#1}{#1}}} \makeatother \def\mycommandhelp#1#2{Mycommands ...


7

\documentclass{article} \newcommand{\myF}{\textit{f}} \newcommand{\myA}{\textit{a}} \newcommand{\yourF}{\begingroup\itshape f\endgroup} \newcommand{\yourA}{\begingroup\itshape a\endgroup} \begin{document} \myF\myA \yourF\yourA \textit{fa} \end{document}


2

Here's a LuaLaTeX-based solution. It defines a Lua function, called hidedelims, that extracts the material surrounded by the delimiters " {L" and "L} ", and it sets up two LaTeX macros, called \switchon and \switchoff, that enable and disable the operation of the Lua function. The term "whitespace" is understood in its generic sense, i.e., to comprise not ...


1

You can replace "{L " by \hideBEG and " L}" by \hideEND by encTeX's primitive \mubyte. Then the removing the argument is a simple TeX task: \mubyte\hideBEG {L \endmubyte % "{L " -> \hideBEG \mubyte\hideEND \space\space L}\endmubyte % " L}" -> \hideEND \newcount\switch \switch=0 \ifnum\switch>0 \def\hideBEG #1\hideEND{\unskip} ...


0

If your main interest is to replace | with \mid if it exists, you can perform a search-and-replace via etoolbox's \patchcmd: \documentclass{article} \usepackage{mleftright,etoolbox,xparse} \makeatletter \DeclareDocumentCommand{\Pr}{R(){}}{% % \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>} ...


3

Control sequence approach As far as you asked about a macro here is a solution using delimited arguments (similar to @jfbu's solution) with a canonical macro introduced through the escape character \, that incorporates your syntax design: \documentclass{article} \newcount\switch \switch=0 \def\maybehide#1{\maybehidei#1} \def\maybehidei L #1 ...


19

A macro is any control sequence (or active character) defined with \def, \gdef, \edef or \xdef. TeX macros support up to nine arguments, which contradicts your statement about it not having the notion of arguments to control sequences. The most common usage of arguments is in the “undelimited” form; say that you do \def\foo#1{--#1--} so \foo takes an ...


13

{} have two distinct uses in TeX, as grouping and to delimit macro arguments (when they do not form a group). Ignoring latex specifics and tests that make the argument optional, then \foo[arg1]{arg2}{arg3} is defined in tex primitives as \def\foo[#1]#2#3{something using #1, #2 and #3} so a use such as \foo[abc]{xyz}{123} then #1 is abc as the ...


3

The simplest is to use the concept of delimited macros: this is not widely documented in LaTeX manuals, but LaTeX is built upon the TeX macro language which allows this functionality. However doing exactly as you want would be problematic (we don't want to change the catcode of the braces), thus, if you accept to delimit the optional part in the following ...


0

Here are basic building blocks. Frankly, it's a whole lot easier letting tabularx do all the work. \documentclass[12pt,a4paper,BCOR5mm]{scrbook} \usepackage{xcolor}% loaded by colortbl \usepackage{graphicx}% loaded by rotating \definecolor{Gray}{gray}{0.9} \fboxsep=0pt \begin{document} How to create a box of a given width (1em) with a colored ...


2

MWE \documentclass{article} \makeatletter \newcommand\@mytitle{} % create macro for title \newcommand\mytitle[1]{\renewcommand\@mytitle{#1}} \newcommand\@mythanks{} % create macro for thanks note \newcommand\mythanks[1]{\renewcommand\@mythanks{% \gdef\@thefnmark{*}\@footnotetext{#1}} } \newcommand\@myauthor{} % create ...


6

etoolbox provides exactly this functionality via \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}, where it searches for <search> in <cmd> and replaces it with <replace>, executing <success> if the search-and-replace was successful, otherwise <failure>. The following uses an elementary ...


5

book, report and article are all generated from the same source (classes.dtx) and the differences that you mention are essentially the only differences there are. If they were no such differences then essentially there would only be one standard class. By design, article omits the \chapter level, to make it slightly easier to combine articles into a report ...


3

This answer actually doesn't answer your question which is about a command / mechanism / programming-trick that can be issued in the body of a document in order to affect how things get defined whose definition-texts occur in the preamble of the document. Your request is due to the unwillingness to scroll between the preamble and, e.g., the middle of a ...


0

You can probably use a combination of \bracestripexchangeand \expandafter: \newcommand\bracestripexchange[2]{#2#1} \newcommand{\sss}{% label={$\bullet$},topsep=0mm, parsep=0mm,% leftmargin=3mm,rightmargin=0cm,labelwidth=5mm,labelsep=3mm% } \expandafter\bracestripexchange\expandafter{\expandafter[\sss]}{\begin{itemize}}% \item gugus \end{itemize}


4

Here is a solution that should cover most of the cases. First, I defined \C to simply set the \parindent to 0 for testing purposes. Then the basic idea is to delay the execution of \C until the begin of the document using the standard interface command \AtBeginDocument. Inside of it the conditional testing is done. When \ifC is set to true it will actually ...


1

\newif\ifC \def\C{\ifC{your definition of C} \else \fi} And now you activate your command setting \Ctrue and desactivate, setting \Cfalse.


2

If you have to set existing TikZ keys with the parser then you can use style args. If you need a custom code to be executed then you can also use code args and proceed from there. For the specific example, a possible way to do it: \tikzset{angles/.style args={#1,#2}{start angle=#1, end angle=#2}} \def\MyCircle[#1]#2{\draw #2 arc [#1];} ...


0

it appears that lyx ignores grouping braces when a file is reloaded. this is fatal when actual [brackets] occur in optional arguments. (it should also be considered a design flaw in lyx.) (la)tex provides the pair \lbrack and \rbrack to substitute for these. the original reason was to accommodate keyboards that don't have square brackets (sometimes the ...


1

You need to insert the uppercasing into the result of \ac, but \ac does not work by expansion so \expandafter does not help. (If it had worked by expansion, your expression would still have been wrong as it passes \empty to \MakeUppercase) \documentclass[a4paper, 10pt]{report} \usepackage[utf8]{inputenc} \usepackage[printonlyused]{acronym} ...


9

True, only a single argument is given to \mathpalette within the \raisemath macro. The second argument to \mathpalette is grabbed from the remainder of the input stream. Here's a smaller example exhibiting the same setup: \documentclass{article} \newcommand{\abcd}[1]{\defg{#1}} \newcommand{\defg}[2]{#1-#2} \begin{document} \abcd{1234}{5678} \end{document} ...


3

I had my answer from August 24 deleted: It actually did not handle the request "if edition = 40,60,80,100,200,...1000 there is also prefix me-". Instead it handled the request "if edition > 21 and natural numbers k < 10 and l exist so that edition = k*(10^l), then there is also prefix me-". Thus, e.g., cases edition=30,50,70,90 were treated ...


1

Box the listing before using it in the argument for \node: \documentclass{beamer} \usepackage{tikz} \usepackage{listings} \newcommand{\rednode}[1]{ \node[draw=red]{#1}; } \newsavebox\mybox \begin{document} \begin{lrbox}{\mybox} \begin{lstlisting} int main() { printf("Hello World\n"); } \end{lstlisting} \end{lrbox} \begin{frame}[fragile] ...


4

You can use \surroundwithmdframed[<options>]{<environment>} A complete example: \documentclass{article} \usepackage{listings} \usepackage{xcolor} %custom colours \usepackage[framemethod=tikz]{mdframed} %nice frames \definecolor{light-gray}{gray}{0.95} %the shade of grey that stack \surroundwithmdframed[backgroundcolor=light-gray, ...


1

Usually a key value parser needs to see the syntax characters = and , directly. Therefore, the macro \sss should be expanded once. This can be done with lots of \expandafter: \expandafter\begin\expandafter{\expandafter i\expandafter t ...\expandafter}\expandafter[\sss] or the whole expression can be expanded as macro: \edef\next{% ...


3

keyval parsers typically don't expand the arguments (otherwise things like font=\small would blow up badly as \small would expand too early. so the simplest thing is to use \newenvironment{sss}{% \begin{itemize}[label={$\bullet$},topsep=0mm, parsep=0mm,% leftmargin=3mm,rightmargin=0cm,labelwidth=5mm,labelsep=3mm% ]} {\end{itemize}} \begin{sss} \item gugus ...


4

The simplest method offered by LaTeX is the \verb macro and the verbatim environment. So for example you can write \verb|print($hello)| and obtain print($hello). Note that instead of regular brackets, \verb's argument is delimited by whichever character you put right after it. This allows you to avoid escaping the delimiters by just choosing a delimiter that ...


3

The solution using \ifnums and using \variants{num}{list}{true-text}{false-text} macro which expands to the "true-text" if num is equal to the number in the list else it expands to the "false-text". \def\variants#1#2{\variantsA{#1}#2,,\end} \def\variantsA#1#2,{\ifx,#2,\expandafter\variantsE \else \ifnum#1=#2 \variantsC \else ...


5

The \prescript command takes three parameters, the third of which is the thing to which the pre-scripts should be attached. Hence \newcommand{\AMod}[1]{\prescript{}{#1}{\text{Mod}}} should work fine. In your attempt the call \prescript{}{#1}\text{Mod} parses as \prescript{}{#1}{\text}{Mod} which means that the third argument is just the macro \text. ...


10

Full form \newcommand{\closure}[1]{\overline{#1}} \overline does not like \par. It throws the error: ! Missing $ inserted. Therefore, it makes sense to include the restriction to \closure by using non-\long arguments: \newcommand*{\closure}[1]{\overline{#1}} Now the error message is: Runaway argument? {... ! Paragraph ended before \closure was ...


0

Although you cannot, as far as I can tell, configure endfloat to recognise the command \addfig appropriately, you can configure it to recognise additional floating environments such as addfig. For example: \begin{filecontents}{endfloat.cfg} \DeclareDelayedFloatFlavor{addfig}{figure} \end{filecontents} \documentclass{article} \usepackage{graphicx} ...


0

In both cases, if the argument found in this way has the form '{}', where stands for any sequence of tokens that is properly nested with respect to braces, the outermost braces enclosing the argument are removed and the will remain. [...] I also want to add that I am less interested in workarounds than in the answer to the question in the title: ...


9

You need to limit the scope of the redefinitions of \arraystretch and \tabcolsep. Since the scope is supposed to be a single table environment, you may achieve your objective by inserting the instructions immediately after the relevant \begin{table} instruction; the scope of the redefinitions ends when \end{table} is encountered. .... \begin{table} % ...


3

Sometimes I use #{-syntax for a macro \Name which allows me both to define and to use control sequence tokens whose names need to be constructed from strings/sequences of character-tokens by means of \csname..\endcsname: \def\Exchange#1#2{#2#1}% \def\Name#1#{\romannumeral0\Innername{ #1}} \def\Innername#1#2{% ...


2

This answer is based on a similar SO question. You can actually use the C preprocessor if you like: $ cat <<EOF | cpp -P | pdflatex #define NAME Sean Allred \documentclass{article} \begin{document} Hello, NAME. \end{document} EOF All of the features of the C preprocessor are available for use :-)


3

The posted example is completely misleading and runs without error. If the array package is used, then macros in the alignment are not expanded, and \newcolumntype can be used for abbreviated column alignment constructs. \documentclass{article} \usepackage{array} \begin{document} \newcolumntype{a}{|l|c|} \begin{tabular}{a} 1 & 2 \\ \end{tabular} ...


4

As Heiko pointed out in his comment, \textbf is eating \else as argument, and \textit takes \fi. You thus need \expandafter in order to tell TeX to wait and finish the if-statement before expanding \textbf and \textit. You should then use \ifnum1=1\relax\expandafter\textbf\else\expandafter\textit\fi{Foo} or \ifnum1=1 ...


4

There is a usage of \edef with parameter in special expansion control. One nice (yet quite complicated) example can be found in skeyval-core.tex v1.3 on line 1997 \skvrobustdef*\skvpopfunctions#1#2{% \begingroup \edef\skv@elt{\skvnoexpandcs{\skvremovescape{#1}@skv@stack}}% \expandafter\skvifdefTF\skv@elt{% \expandafter\ifx\skv@elt\@empty ...


5

After a completely false understanding of the question, I have rewritten my answer to show how \edef with an argument can be used to "fix" the selected equation citation style. An \edef is evaluated at time of definition, whereas \def is evaluated at time of execution. If \edef\eqcite#1{} is used in preference to \def\eqcite#1{}, then changes to the ...


3

Some time ago I got this \usepackage{etoolbox} \letcs\originald{\encodingdefault\string\d} \DeclareRobustCommand*\d {\ifmmode\mathop{}\!\mathrm{d}\else\expandafter\originald\fi} and, without \letcs \expandafter\let\expandafter\originald\csname\encodingdefault\string\d\endcsname \DeclareRobustCommand*\d ...


12

Searching for \edef or \xdef (global variant) with command to be defined and argument in TeX Live 2015 and filtering out possible false positives (when the command is constructed with \csname): egrep -r '\\[ex]def\\[a-zA-Z@]+#1' texmf-dist/tex/ | grep -v '\\[ex]def\\csname' Result are 219 lines. I do not want to discuss all findings, thus I restrict ...


5

Here's an implementation in expl3: \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \cs_new:Nn \georgian_ordinal:n { \int_compare:nTF { 1 < #1 < 21 } { me\mbox{-}#1 } { \georgian_ordinal_aux:n { #1 } } } \cs_new:Nn \georgian_ordinal_aux:n { \int_case:nnF { #1 } { { 1 }{ #1\mbox{-}li } { 40 }{ me\mbox{-}#1 } ...


1

Here's a modification of the former solution that uses a different workaround: \documentclass{article} \usepackage{amsmath} \usepackage{color} \usepackage{xstring} \makeatletter \newcommand{\ChgFmt}[1]{% \ensuremath{% \begingroup \expandarg \protected@edef\temp{#1}% \IfSubStr{\temp}{-} {\StrSubstitute{\temp}{-}{}[\temp]\@firstoftwo} ...


2

Would this one-line parsing suffice, without use of xstring? This problem is really tailor-made for an \if-#1 construct for two reasons: \if fully expands its arguments before the test (which matters because the - is embedded several expansions down inside #1); and a "positive" test strips the leading token from the argument, which is exactly what the ...


10

miniltx was a few minutes work, I just copied things from the latex.ltx source of the latex format until an example using \includegraphics worked in plain TeX. So you need to look at the source of latex.ltx which was written by several people over several years. In this case % % \begin{macro}{\@gobble} % \begin{macro}{\@gobbletwo} % ...


3

Wouldn't you prefer to test \foo directly, rather than through the intermediate definition of \test? \documentclass[11pt]{article} \def\foo#1{\relax} \let\svfoo\foo \def\testfoo{\ifx\svfoo\foo true\else false\fi} \begin{document} \testfoo \renewcommand{\foo}[1]{{\bfseries #1}} \testfoo \renewcommand{\foo}[1]{{\ttfamily #1}} \testfoo ...



Top 50 recent answers are included