Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to create a simple macro using TeX with optional parameters:

 \elide[options]{}

I understand a common way is to use \futurelet. So far the code is as follows:

\documentclass[11pt]{article}
\begin{document}

\def\elidebefore[#1]#2{[$\ldots$] #2}
\def\elideafter#1{#1$\ldots$}

\def\elide {%
\futurelet\ifoptions
    \choosemacro
}

% The \choosemacro, based on
% the lookahead of \elide, calls
% either \elidebefore or \elideafter 


\def\choosemacro{%
 \ifx\ifoptions [%
     \let\choice = \elidebefore 
 \else
    \let\choice = \elideafter
 \fi
\choice
}

Testing \elide[b]{Lorem ipsum}

\elide{Lorem Ipsum}

\elide[b]{Lorem ipsum}

\end{document}

This works ... but since I am only checking if the square bracket exists and switching to either macro1 or macro2 elide[]{} will work with anything in-between the square brackets. How, can I extend the macro to read the letters between the square brackets?

Is there a predefined LaTeX macro that one can use? (I know about keyval, but I just prefer to do this with basic commands).

share|improve this question
1  
I am a bit confused over what you mean by extending the macro to read the letters between the square brackets. You do have the text between the brackets available as #1, after all. You only need to use it in the body of your macro. –  Harald Hanche-Olsen Oct 25 '10 at 20:13
    
@Harald Hanche-Olsen Thanks for the answer and the comment. If you try \elide[]{test} it works. I am trying to extend the macro so that one would be able to parse something like \elide[a,b,c]{} or \elide{}. I am also trying to understand the workings of \futurelet a bit better. –  Yiannis Lazarides Oct 25 '10 at 20:19
    
Okay, but what do you want \elide[a,b,c]{} to do? –  Harald Hanche-Olsen Oct 25 '10 at 20:37
    
@Harald Hanche-Olsen \elide[b,s]{text} -> [...]text (b=before,s=square brackets), \elide[a,b]{text}-> text[...], but primarily I am trying to relearn to program TeX/LaTeX, for an application I am building! \futurelet just tied my brain in knots:) –  Yiannis Lazarides Oct 25 '10 at 20:59
    
Not really what you're trying to do. But for an example of an application of \futurelet see this question. –  Juan A. Navarro Oct 25 '10 at 21:21

1 Answer 1

up vote 5 down vote accepted

If you are only trying to define a macro with an optional argument, \newcommand will do that for you:

\newcommand{\elide}[2][\relax]{%
  \ifx\relax#1%
    #2$\ldots$%
  \else $\ldots$ #2\fi}

should work the same as your macro, except it may behave oddly if you give it an optional argument beginning with \relax.

share|improve this answer
3  
This is a really bizarre use of an optional argument. The value is irrelevant. If this were the use case, I'd say go with a * instead. –  TH. Oct 26 '10 at 2:40
    
Agreed. But assuming that I want to use the optional argument, in the case where it is present, and do something entirely different where it is not, this technique has worked well for me. –  Harald Hanche-Olsen Oct 26 '10 at 10:06
    
@TH. of course, then \def\elide{\@ifnextchar*{<star-code>\@gobble}{<non-star-code>}} works just good (of course with \makeat(letter|other)). –  yo' Aug 24 '12 at 22:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.