0

I am writing a custom, package using Texmaker with Pdftex. I have a function, which I would like to change a certain delimiter to whatever the user wishes if my provided delimiters are not good, or not preferable. example is as follows.

I have a Switch Case Function for which I would like to be able to change the delimiter "-" either with predefined delimiters, or with a user input delimiter if they happen to need all of the provided delimiters.

The best case would be to remove all of the predefined delimiters and enable the user to type \usepackage[SwitchCaseDelimiter=""]{Customs} and use it that way.

\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{Customs}[2019/16/12 Custom Function Package]
\RequirePackage{xparse}
%\RequirePackage{tikz}
%\RequirePackage{pdfpages}
%\RequirePackage{graphicx}
%\RequirePackage{capt-of}
%\RequirePackage{caption}
%\RequirePackage{geometry}
%\RequirePackage{catchfile}

\newcommand{\SCdelim}{,}

\DeclareOption{Semicolon}{\renewcommand{\SCdelim}{;}}
\DeclareOption{Colon}{\renewcommand{\SCdelim}{:}}
\DeclareOption{Exclaimation}{\renewcommand{\SCdelim}{!}}
\DeclareOption{Period}{\renewcommand{\SCdelim}{.}}
\DeclareOption{Minus}{\renewcommand{\SCdelim}{-}}
\DeclareOption{Equals}{\renewcommand{\SCdelim}{=}}
\DeclareOption{Carrot}{\renewcommand{\SCdelim}{^}}
\DeclareOption{LeftArrow}{\renewcommand{\SCdelim}{<}}
\DeclareOption{RightArrow}{\renewcommand{\SCdelim}{>}}
\DeclareOption{Arrows}{\renewcommand{\SCdelim}{<>}}
\DeclareOption{Other}{\renewcommand{\SCdelim}[1]{#1}}

\ProcessOptions\relax
\makeatletter
\newcommand\SwitchCase[2]{%
  \def\tmpa{#1}%
  \@for\tmp:=#2\do{\expandafter\zz@switch\tmp\zz@switch}%
 }
 \def\zz@switch#1-#2\zz@switch{% ideally change the delimiter in this line
  \def\tmpb{#1}%
   \ifx\tmpa\tmpb#2\fi}

\makeatother
\endinput

What I wanted to do was the following:

 \def\zz@switch#1\SCDelim#2\zz@switch{% ideally change the delimiter in this line
  \def\tmpb{#1}%
   \ifx\tmpa\tmpb#2\fi}

The excessive amount of headers is merely to show what other headers are used in the package. there are about 5 or 6 more functions in the package but they are not relevant to the question. I only included my required packages so that any answers would not cause conflicts or errors with the packages already in use.

P.S. I apologize for any awkward wording, or weird coding.

5
  • 1
    that should work (although Other definition looks like it will cause issues later in the file) , have you tried it, what is the actual question? (please provide a test file that can be used to show any problems) Dec 19, 2019 at 16:09
  • 1
    al alternative interface would use a keyval system (eg kvoptions package) then have a single option of the form [delim=!] or [delim={=}] etc Dec 19, 2019 at 16:12
  • If possible, i would like to avoid requiring too many packages. I have 8 packages in this file alone, and my other documents have even more.
    – cdickstein
    Dec 19, 2019 at 16:25
  • 1
    you have edited the question since my initial comments, and now ask for a key=value synatax, that's exact;y what kvoptions provides so i can't see any reason not to use that. then you would only need one line to define the option. Dec 19, 2019 at 16:35
  • ok, thanks I will try that. also, I edited the question after you requested more to work with in the first comment.
    – cdickstein
    Dec 19, 2019 at 16:37

1 Answer 1

1

You are using expl3, so it's much better to exploit its features.

\ProvidesPackage{customs}
\RequirePackage{expl3,xparse,l3keys2e}

\ExplSyntaxOn

\keys_define:nn { customs }
 {
  Other .tl_set:N = \l__customs_delim_tl,
  Comma .code:n = \keys_set:nn { customs } { Other = {,} },
  Semicolon .code:n = \keys_set:nn { customs } { Other = ; },
  Exclamation .code:n = \keys_set:nn { customs } { Other = ! },
  Period .code:n = \keys_set:nn { customs } { Other = . },
  Hyphen .code:n = \keys_set:nn { customs } { Other = - },
  Equals .code:n = \keys_set:nn { customs } { Other = {=} },
  Caret .code:n = \keys_set:nn { customs } { Other = ^ },
  LeftArrow .code:n = \keys_set:nn { customs } { Other = < },
  RightArrow .code:n = \keys_set:nn { customs } { Other = > },
  Arrows .code:n = \keys_set:nn { customs } { Other = <> },
  Colon .code:n = \tl_set:Nx \l__customs_delim_tl { \token_to_str:N : },
 }

\keys_set:nn { customs } { Comma }
\ProcessKeysOptions { customs }

\seq_new:N \l__customs_switch_seq
\cs_generate_variant:Nn \seq_set_split:Nnn { NV }

\NewDocumentCommand{\SwitchCase}{mm}
 {
  \seq_set_split:NVn \l__customs_switch_seq \l__customs_delim_tl { #2 }
  \seq_if_in:NnT \l__customs_switch_seq { #1 } { #1 }
 }

\ExplSyntaxOff

\endinput

Here are some test files.

1. No option

\documentclass{article}

\usepackage{customs}

\begin{document}

X\SwitchCase{abc}{x,abc,d}X

X\SwitchCase{abc}{x-abc-d}X

X\SwitchCase{abc}{xyyabcyyd}X

\end{document}
  1. Option Hyphen
\documentclass{article}

\usepackage[Hyphen]{customs}

\begin{document}

X\SwitchCase{abc}{x,abc,d}X

X\SwitchCase{abc}{x-abc-d}X

X\SwitchCase{abc}{xyyabcyyd}X

\end{document}
  1. Option Other
\documentclass{article}

\usepackage[Other=yy]{customs}

\begin{document}

X\SwitchCase{abc}{x,abc,d}X

X\SwitchCase{abc}{x-abc-d}X

X\SwitchCase{abc}{xyyabcyyd}X

\end{document}

Output of 1

enter image description here

Output of 2

enter image description here

Output of 3

enter image description here

1
  • I do appreciate and see how the answer works, but the current version of switch case I have is the one I know works, as I give it a case and an action, almost like using delegates. I do have a reason for doing that, but for now it seems that there is no way other than using predefined delimiters. Because my switch case takes 2 delimiters, the above answer does not work, I only need to change one of those delimiters though.
    – cdickstein
    Dec 19, 2019 at 17:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .