27

I want to use some text as the optional parameter to some command. The text includes square braces ([ and ]), but since the optional parameter goes within square brackets, unexpected things happen.

More info: I want an itemized list whose bullets are citations like [GMR85]. I used this command:

\begin{itemize}
  \item[[GMR85]] ...
\end{itemize}

but it takes the first ] as the end of optional parameter.

Any way to escape ] ?

Edit: I specially want this to work in beamer. My current result is like:

Note how ] is not included the label.

EDIT 2

This bug has been solved in the latest versions of beamer.

diabonas
  • 25,784
Sadeq Dousti
  • 4,380
  • 3
    Use {description} rather than {itemize} with lexicon-like lists. – Charles Stewart Dec 07 '10 at 13:23
  • Also, why don't you use the bibliography environment? – Caramdir Dec 07 '10 at 17:51
  • @Caramdir: I always use the bibliography environment, but let's just say that there are technical reasons I don't want to do that now. (It's a long story!). As a short answer, you can just assume that I want an enumerate listing whose labels include brackets. (while they aren't citations.) – Sadeq Dousti Dec 07 '10 at 18:26
  • @Charles: Sorry, but even {description} environment does not work in the beamer. (Screenshot: http://s1.bild.me/bilder/311010/1151044untitled.PNG) – Sadeq Dousti Dec 07 '10 at 18:37
  • I found a way to solve your beamer problem, please see my solution below... – Matten Dec 07 '10 at 18:59
  • 4
    I'm voting to close this question as off-topic because this problem has been fixed in a newer version of beamer. – LaRiFaRi Jul 19 '15 at 07:57
  • In the general case https://tex.stackexchange.com/questions/99495/inside-an-optional-argument covers it. – user202729 Apr 01 '23 at 12:27

2 Answers2

29

group the citation...

\documentclass{article}
\begin{document}

\begin{itemize}
  \item[{[GMR85]}] ...
\end{itemize}
\end{document}

The Beamer package makes things a little bit more difficult.

\documentclass{beamer}
\begin{document}

\def\braces#1{[#1]}

\begin{frame}{frame title}
\begin{itemize}
  \item[\braces{GMR95}] ...
\end{itemize}
\end{frame}

\end{document}

This works fine for me.

Matten
  • 5,687
  • Thanks Matten, but it does not work either (I tried) – Sadeq Dousti Dec 07 '10 at 13:01
  • 2
    It does not work? I'll put a complete example in my answer, I have compiled it and it works fine for me... – Matten Dec 07 '10 at 13:04
  • 1
    itemize will cause the bullet cross beyond the left margin. See my answer (not really answer actually, just a comparison) :-) – Display Name Dec 07 '10 at 17:43
  • @Matten: Sorry, I should have described more. I'm using it in a beamer slide, and it does not work in that case (but works fine when you use the article documentclass). Can you test it in the beamer too? (screenshot of my result: http://s1.bild.me/bilder/311010/1151044untitled.PNG). – Sadeq Dousti Dec 07 '10 at 18:35
  • hey Sadeq.. I supplied another approach for your beamer problem. Please see my second code example! – Matten Dec 07 '10 at 18:55
  • @Matten: Thanks a lot, it works. It will be nice if somebody can describe what's wrong with Beamer. – Sadeq Dousti Dec 07 '10 at 20:16
  • You can submit an issue on the current development project on beamer with your bug. http://bitbucket.org/rivanvx/beamer/wiki/Home – Matten Dec 07 '10 at 20:37
  • @Matten: Thanks. I filed the bug here: http://bitbucket.org/rivanvx/beamer/issue/102/. – Sadeq Dousti Dec 08 '10 at 00:47
  • Using grouping with {}'s worked for to solve a similar problem with including square brackets for the description of a beamer definition environment. – Sam Buss Nov 04 '18 at 21:38
2

Based on Charles Stewart's comment and Matten's answers, I make the following comparion. I suggest to follow the Stewart's suggestion. If we insist on using itemize environment instead of description, the bullet item will expands to the left crossing the left margin.

alt text

\documentclass[dvipsnames]{article}
\usepackage{xcolor}
\usepackage[showframe=true,margin=30mm]{geometry}
\usepackage{showexpl}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\SX@codeInput}{xleftmargin=0pt,xrightmargin=0pt}{}
  {\typeout{***Successfully patched \protect\SX@codeInput***}}
  {\typeout{***ERROR! Failed to patch \protect\SX@codeInput***}}
\makeatother
\lstset{%
  literate={ï}{}0
         {»}{}0
         {¿}{}0,
    breaklines=true,
    breakindent=0pt,    
    basicstyle=\ttfamily\scriptsize,
    keywordstyle=\color{blue}\sffamily\bfseries,
    commentstyle=\color{Green}\itshape,                                 
    stringstyle=\rmfamily,                          
    showstringspaces=false,
    backgroundcolor=\color{Yellow!30},
    frame=single,
    framerule=0.4pt,
    rulecolor=\color{red},
    framesep=3pt,
    xleftmargin=3.4pt,
    xrightmargin=3.4pt,
    tabsize=2,%
    explpreset={pos=b}%
}
\begin{document}

\begin{LTXexample}
\begin{description}
  \item{[GMR85]} I am happy.
  \item{[GMR86]} Are you happy?
\end{description}
\end{LTXexample}


\begin{LTXexample}
\begin{itemize}
  \item[{[GMR85]}] I am happy.
  \item[{[GMR86]}] Are you happy?
\end{itemize}
\end{LTXexample}

\end{document}
Display Name
  • 46,933