0

I want to use \csname inside \IfStrEqCase but I have an error. Here is my code:

\documentclass[a4paper,12pt,hidelinks]{article}
\usepackage[utf8]{inputenc} 
\usepackage[brazil]{babel}
\usepackage[T1]{fontenc}

\usepackage{filecontents}
\usepackage{xstring}
\usepackage{alphalph}
\usepackage{etoolbox}
\usepackage{tikz}
\usepackage{forloop}

%dados-a
\newcommand {\auxsistelet}[3]{\IfStrEq{#2}{BICICLETA}{\IfStrEqCase{#1}    {{}{Seus sistemas de segurança para o tráfego (freio e direção) atuavam a     contento}{n}{Seus componentes de segurança para o tráfego (direção e     freio) não puderam ser testados devido à magnitude dos danos ocasionados     pelo acidente}}[Seus sistemas de segurança para o tráfego (freio e     direção) atuavam a contento, excetuando-se #1, face aos danos sofridos no     evento]
}{
\IfStrEqCase{#1}{{}{Seus sistemas de segurança para o tráfego (freio,     direção e elétrico) atuavam a contento}{n}{Seus componentes de segurança     para o tráfego (direção, freio e elétrico) não puderam ser testados devido     à magnitude dos danos ocasionados pelo acidente}}[\IfStrEq{#1}{chave}    {\IfStrEq{#3}{s}{Seus componentes de segurança para o tráfego (direção,     freio) funcionavam a contento. O sistema elétrico não pôde ser testado     devido à ausência da chave do referido veículo}{Seus componentes de     segurança para o tráfego (direção, freio e elétricos) não puderam ser     testados devido à ausência da chave do referido veículo. Da mesma forma,     fica prejudicada a análise de sua porção interna}}{Seus sistemas de     segurança para o tráfego (freio, direção e elétricos) atuavam a contento,     excetuando-se #1, face aos danos sofridos no evento}]}.}

%dados
\newcommand \placa {AAA-0000}
\newcommand \eletrico{\sistelet{n}\\}
\newcommand \tipoveiculo {AUTOMÓVEL}
\newcommand \moto {n}

\newcommand \placab {BBB-0000}
\newcommand \eletricob{\sisteletb{}\\}
\newcommand \tipoveiculob {MOTOCICLETA}
\newcommand \motob {s}

\begin{document}

\ifdef{\placa}{\newcommand \sistelet[1]{\auxsistelet{#1}{\tipoveiculo}    {\moto}}}{}
\ifdef{\placab}{\newcommand \sisteletb[1]{\auxsistelet{#1}    {\tipoveiculob}{\motob}}}{}

% \foreach \x in {b,...,e}{
% \ifcsdef{placa\x}{\expandafter\global\newcommand \csname sistelet\x\endcsname[1]{\csname auxsistelet\x\endcsname{#1}{\csname     tipoveiculo\x\endcsname}{\csname moto\x\endcsname}}}{}
% }

\ifdef{\eletrico}{\eletrico}{}
\ifdef{\eletricob}{\eletricob}{}
\end{document}

I want to change this:

\ifdef{\placab}{\newcommand \sisteletb[1]{\auxsistelet{#1}    {\tipoveiculob}{\motob}}}{}

For something like this:

\foreach \x in {b,...,e}{
\ifcsdef{placa\x}{\expandafter\global\newcommand \csname sistelet\x\endcsname[1]{\csname auxsistelet\x\endcsname{#1}{\csname     tipoveiculo\x\endcsname}{\csname moto\x\endcsname}}}{}
}

How can I do this?? Help me!

2
  • Perhaps you should explain first what your real intentions are here. I see a bunch of \csname...\endcsname and (most probably) wrongly placed }
    – user31729
    Apr 5 '16 at 20:03
  • I want to make a code for to be better than create multilines for \placab \placac \placad and \placae Apr 5 '16 at 20:35
2

I don't understand what you are trying to do but \expandafter\global\newcommand is wrong and you probably wanted \global\expandafter\newcommand\csname...\endcsname[1]{...}, but that can't work either because \newcommand checks non expandably for various things and the \global will be without effect. But

\long\global\expandafter\def\csname...\endcsname #1{...}

works. As I commented at your other question how to use a \global \foreach?, you can use \csgdef as you already load etoolbox. Or, without package, as you are using LaTeX:

\makeatletter
\long\global\@namedef{...}#1{...}
\makeatother

If used within macros replace #1 in the definitions above by ##1. (and the \makeatletter/\makeatother pair must not be inside the macro but surround it).

1
  • the use case was and still is far from clear, but an addendum must be made: an \x from a TikZ \foreach loop will not be expanded in time if it occurs in the replacement text of the defined macro. As pointed out by @PeterGrill tex.stackexchange.com/questions/302718/…, something like \csxdef is probably needed. With the suitable \noexpand's.
    – user4686
    Apr 7 '16 at 16:47
0

Solved by this way:

\ifdef{\placa}{\newcommand \sistelet[1]{\auxsistelet{#1}{\tipoveiculo}{\moto}}}{}
\ifnumcomp{\nveics}{>}{1}{
\foreach \x in {b,...,\alphalph{\nveics}}{
\ifcsdef{placa\x}{\csgdef{sistelet\x}##1{\auxsistelet{##1}{tipoveiculo\x}{moto\x}}}{}
}
}{}
2
  • Indeed of posting a self-answer you should rather accept jfbu's answer who pointed you into the right direction with \csgdef etc.
    – user31729
    Apr 7 '16 at 15:40
  • are you sure this works? the \x from tipoveiculo\x will not be expanded by the \csgdef. This was pointed out by @PeterGrill tex.stackexchange.com/questions/302718/…
    – user4686
    Apr 7 '16 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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