2010-03-08 18 views
14

Sto cercando di costruire un comando che è simile al lattice \cite{}, che accetta un elenco separato da virgole di parametri come questoSplit parametri separati da virgola in LaTeX

\cite{Wall91, Schwartz93}

vorrei passare ogni elemento nell'elenco separato da virgole che il parametro rappresenta per un altro comando e restituisce la concatenazione dei singoli risultati. Immagino che sia qualcosa di simile:

\newcommand{\mycite}[1]{% 
    \@for\var:=\split{#1} do{% 
    \processCitation{\var}% 
    }% 
} 

letteratura sulla stringa di manipolazione, le variabili e loop in LaTeX sarebbe grande!

Inoltre: esiste un modo per unire nuovamente i singoli risultati utilizzando le virgole?

Grazie!

risposta

17

Utilizzando collegamento di Roberto sono arrivato a questa soluzione: esempio

\makeatletter 

% Functional foreach construct 
% #1 - Function to call on each comma-separated item in #3 
% #2 - Parameter to pass to function in #1 as first parameter 
% #3 - Comma-separated list of items to pass as second parameter to function #1 
\def\foreach#1#2#3{% 
    \@[email protected]{#1}{#2}#3,\@[email protected]% 
} 

% Internal helper function - Eats one input 
\def\@swallow#1{} 

% Internal helper function - Checks the next character after #1 and #2 and 
% continues loop iteration if \@[email protected] is not found 
\def\@[email protected]#1#2{% 
    \@ifnextchar\@[email protected]% 
    {\@swallow}% 
    {\@foreach{#1}{#2}}% 
} 

% Internal helper function - Calls #1{#2}{#3} and recurses 
% The magic of splitting the third parameter occurs in the pattern matching of the \def 
\def\@foreach#1#2#3,#4\@[email protected]{% 
    #1{#2}{#3}% 
    \@[email protected]{#1}{#2}#4\@[email protected]% 
} 

\makeatother 

Usage:

% Example-function used in foreach, which takes two params and builds hrefs 
\def\makehref#1#2{\href{#1/#2}{#2}} 

% Using foreach by passing #1=function, #2=constant parameter, #3=comma-separated list 
\foreach{\makehref}{http://stackoverflow.com}{2409851,2408268} 

% Will in effect do 
\href{http://stackoverflow.com/2409851}{2409851}\href{http://stackoverflow.com/2408268}{2408268} 
+0

E come si usa questo? Potresti fare un esempio? – AVB

+0

Grazie per l'esempio! +1 per domande e risposte entrambe. Ti dispiace dare un'occhiata qui: http://stackoverflow.com/questions/2389081/ Forse avrai un'idea. – AVB

+0

Nessun problema. Grazie per il voto. Dai un'occhiata alla soluzione che ho dato nel tuo post! –

-3

È possibile utilizzare il pacchetto cite come

\usepackage{cite} 
... 
\cite{citation1, citation2, citation3} 

Controllare questo link.

+3

Ci dispiace, ma la domanda è come costruire un comando come citare te stesso. –

Problemi correlati