4

I'm trying to find a LaTeX command that allows me to repeat a specified character (like quotation marks) a certain number of times around a given text. For example, I want to define a command like \quote[n]{y}, which would output n repetitions of a character surrounding (the quotations marks in this case) the text y.

Is there a package with a command doing this job? If not, how could I define it?

Note that, in LaTeX, n repetitions of the quotations marks would have n times repetions of the left quotation mark (i.e., `) at the left and n repetion of the right quotation mark (i.e., ') at the right. Alternatively, it could be n repetions of the command \enquote{}, with suitable adaptations (each repetion should onctain only one pair of simple quotation marks).

OP edit: Is there a way to, instead of puting ` at the left, to indicate the left quote of the command \enquote* provided by the csquotes package (and respectively for ')? I say this because I would also like that it be adapted to other languages, like German.

3
  • 1
    Have a look at the pgffor package. Commented Nov 4 at 13:06
  • Also Expl3 has several loops for a defined functionality.
    – MS-SPO
    Commented Nov 4 at 13:10
  • @elmo It is usually frowned upon to change a question after you've already got replies for the previous version of your question. Commented Nov 4 at 16:52

3 Answers 3

11
+50

As egreg has posted an expl3 version here's a tex primitive loop, just for comparison

enter image description here

\documentclass{article}

\begingroup\lccode`~`m\lowercase{\endgroup
\def\Quoted#1#2{%
  $\mathcode`m="8000\def~{\hbox{`}}\romannumeral#1000$%
  #2%
  $\mathcode`m="8000\def~{\hbox{'}}\romannumeral#1000$%
}
}
\begin{document}

\Quoted{1}{abc}

\Quoted{2}{abc}

\Quoted{10}{abc}

\end{document}
9
  • Thans, @David Carlisle. Is there a way to, instead of puting ` at the left, to indicate the left quote of the command \enquote* provided by the csquotes package (and respectively for ')?
    – elmo
    Commented Nov 4 at 16:22
  • This reminds me of the “regular expression” that applies the sieve of Eratosthenes. Commented Nov 4 at 16:23
  • @elmo yes/no I avoided a specific loop by using math mode to give a magic definition for say mmm (which is roamn for 4000 but that rather limits what you coudl do, you could generate mmm and then loop over the m but then you may as well loop from 1 to 4 without the romannumeral trick which is what @egreg shows Commented Nov 4 at 17:03
  • @elmo yes just change the \hbox{`} to \UseName{csq@thequote@oopen} in this answer and you get what csquotes would use.
    – Skillmon
    Commented Nov 4 at 20:03
  • @Skillmon oh I thought the OP needed to generate \enquote{\enquote{...{abc}}...}} which would be harder this way but OK yes if you just need left and right quotes separately, then as you say. Commented Nov 4 at 20:06
12

With expl3 there's \prg_replicate:nn.

We need to insert a zero kern in order to avoid ligatures.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\Quoted}{O{1}m}
 {
  % #1 = number of quote marks (default 1), #2 = text to be quoted
  \prg_replicate:nn {#1} {`\kern0pt}
  #2
  \prg_replicate:nn {#1} {\kern0pt'}
 }

\ExplSyntaxOff

\begin{document}

\Quoted{abc}

\Quoted[2]{abc}

\Quoted[10]{abc}

\end{document}

output

For csquotes:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[french,english]{babel}
\usepackage[autostyle,french=guillemets]{csquotes}

\ExplSyntaxOn

\NewDocumentCommand{\Quoted}{O{1}m}
 {
  % #1 = number of quote marks (default 1), #2 = text to be quoted
  \group_begin: \setquotestyle{\languagename}
  \prg_replicate:nn {#1} {\use:c{csq@thequote@oopen}}
  #2
  \prg_replicate:nn {#1} {\use:c{csq@thequote@oclose}}
  \group_end:
 }

\ExplSyntaxOff

\begin{document}

\Quoted{abc}

\Quoted[2]{abc}

\Quoted[10]{abc}

\selectlanguage{french}

{\enquote*{abc}}

\Quoted{abc}

\Quoted[2]{abc}

\Quoted[10]{abc}

\end{document}

output

3
  • Thanks, @egreg. Is there a way to, instead of puting ` at the left, to indicate the left quote of the command \enquote* provided by the csquotes package (and respectively for ')?
    – elmo
    Commented Nov 4 at 16:21
  • 1
    @elmo See edit.
    – egreg
    Commented Nov 4 at 17:06
  • @egreg sorry, you were robbed Commented Nov 4 at 21:01
5

For the sake of variety, a LuaLaTeX-based solution. Note that because \Quoted uses Lua's own string.rep function, it's not necessary to perform a loop operation.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}

\newcommand{\Quoted}[2][1]{\directlua{tex.sprint ( 
    string.rep ("\\mbox{`}",#1) .. "#2" .. string.rep ("\\mbox{'}",#1) 
    )}}

\begin{document}
\Quoted{abc}

\Quoted[2]{abc}

\Quoted[10]{abc}
\end{document}
3
  • Well, strictly speaking, David's answer doesn't involve a TeX-loop as well.
    – Skillmon
    Commented Nov 4 at 19:51
  • @Skillmon - Thanks. My comment was meant as a glosse on the OP's use of the loops tag. I guess I could have been a bit clearer... Thinking about the topic of loops, I suppose I don't really know how Lua implements the string.rep function -- might it involve executing a for loop?
    – Mico
    Commented Nov 4 at 20:35
  • 1
    It might :) (more characters)
    – Skillmon
    Commented Nov 4 at 20:39

You must log in to answer this question.

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