Once again, I have a macro problem. It works almost. I spend a lot of time googling about \expandafter, \noexpand, \unexpanded and others. I used some of them, but nothing lead to the desired result.
If you find a better title for this problem, change it. I used the best, I could think of.
The following is my code:
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
\usepackage{graphicx}
\usepackage{environ}
\makeatletter
%%%Liederbuch. This works!!!
\NewEnviron{Liederbuch}[1]{
\xdef\LB@my@temp{
\noexpand\newcommand{\csname LH#1\endcsname}[2]{
\unexpanded\expandafter{\BODY}
}
}
\aftergroup\LB@my@temp
}
%%%%%%%%%%%%%%%%%
%The problem starts in the following Environ:
%If I replace `##1` and `##2` with constants, it works, but therefore it fails the basic intention of this effort.
%To see, what should happen, have a look at `\begin{document}`
%%%%%%%%%%%%%%%%%
%%%Lied. This doesn't work!!!
\NewEnviron{Lied}[2]{%
\ifnum\numexpr#2=\numexpr##2 % ##2 doesn't work; i.e. "1" instead of ##2 works
\ifnum\pdfstrcmp{#1}{##1}=0 % ##1 doesn't work; same as above
\BODY
\fi
\fi
}
\makeatother %residual of some tries with xdef and \aftergroup
%%%%%%%%%%%%%%%%%
%Apparently, the following code is super clean. That is the basic idea.
%%%%%%%%%%%%%%%%%
% This is a Liederbuch
\begin{Liederbuch}{songbook}
\begin{Lied}{t}{1}
Song 1 par t
\end{Lied}
\begin{Lied}{n}{2}
Song 2 par n
\end{Lied}
\end{Liederbuch}
% LHsong calls "the environment" This works!!!
\newcommand{\LHsong}[3][n]{
\csname LH#2\endcsname[#1]{#3}
}
\begin{document}
%This should produce Song 1 par t
\LHsong[t]{songbook}{1}
%This shouldn't produce anything
\LHsong[n]{songbook}{1}
%This should produce Song 2 par n
\LHsong{songbook}{2}
%This shouldn't produce anything
\LHsong[t]{songbook}{2}
%This should also produce Song 1 par t
\LHsongbook{t}{1}
\end{document}
The Liederbuch expands to the following code which isn't part of my code
% it expands to: %not part of the code
%Liederbuch
\newcommand{\csname LHsongbook\endcsname}[2]{
%Lied 1
\ifnum1=\numexpr##2 %\numexpr#2 expanded to 1; ##2 should be par2 of newcommand
\ifnum\pdfstrcmp{t}{##1}=0 %#1 expanded to t
Song 1 par t
\fi
\fi
%Lied 2 - same here different values
\ifnum2=\numexpr##2
\ifnum\pdfstrcmp{n}{##1}=0
Song 2 par n
\fi
\fi
}
I stole the Liederbuch-Environ from one of my other questions. The proof, that this works, can be seen by the change of ##x to constants. I suppose, that Lied should look similar, but I couldn't figure out, how.
The intention is, to have environment Liederbuch create a [perhaps series of] custom Lied environment[s] in which an invocation of \LHsong will produce output from Lied if the arguments match certain specifications.
need not use\csname, of course; the arguments should be#1and#2: using##1` is simply wrong. – egreg Apr 14 '16 at 10:23##xto refer to the original code. The problem starts after the last comment line, which is displayed without scrolling down. – MaestroGlanz Apr 14 '16 at 10:25##wrong (also\newcommandand\NewEnvironare not defined to be used that way). Could you expand on what do you want the code to do? – Manuel Apr 14 '16 at 10:36Liederbuchcreate a [perhaps series of] customLiedenvironment[s] in which an invocation of\LHsongwill produce output fromLiedif the arguments match certain specifications. Is that right? – Steven B. Segletes Apr 14 '16 at 10:47\usepackaged. So I can include several Liederbuch-s and choose specific titles from all of them with only one command. – MaestroGlanz Apr 14 '16 at 10:58leadsheetspackage would allow you to print songs only if they match certain tags. So a separate file could contain the song definitions and in the main file said file is\inputand a corresponding setup makes sure only those matching certain tags are actually printed. – cgnieder Apr 14 '16 at 11:10