1

I am working with beamer to make slides. When I try to add some code in lstlist inside an item list, the list is always in the next line compared to the bullet, as in the following picture.

enter image description here

My code is as the following:

\documentclass[aspectratio=169]{beamer}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{listings}
\usepackage{lstautogobble}

\mode<presentation>
{
  \usetheme{Warsaw}
  \setbeamercovered{transparent}
}

\usecolortheme{crane}
\useoutertheme{split}
\usefonttheme[onlysmall]{structurebold}

\lstset{
    basicstyle=\tiny \ttfamily,
    frame=single,
}

\begin{document}
    \section{listing in item}
        \begin{frame}[t, fragile]{Listing in Item}
            \begin{onlyenv}<+>
                \begin{itemize}
                    \item
                        \begin{lstlisting}[autogobble]
                            How to align this with the bullet? 
                        \end{lstlisting}
                \end{itemize}
            \end{onlyenv}
        \end{frame}
\end{document}

Is it possible to raise the list one line above to align to the bullet?

2

Quick workaround: change the aboveskip of your listing

\documentclass[aspectratio=169]{beamer}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{listings}
\usepackage{lstautogobble}

\mode<presentation>
{
  \usetheme{Warsaw}
  \setbeamercovered{transparent}
}

\usecolortheme{crane}
\useoutertheme{split}
\usefonttheme[onlysmall]{structurebold}

\lstset{
    basicstyle=\tiny \ttfamily,
    frame=single,
}

\begin{document}
    \section{listing in item}
        \begin{frame}[t, fragile]{Listing in Item}
            \begin{onlyenv}<+>
                \begin{itemize}
                    \item
                        \begin{lstlisting}[autogobble,aboveskip=-.8\baselineskip]
                            How to align this with the bullet? 
                        \end{lstlisting}
                \end{itemize}
            \end{onlyenv}
        \end{frame}
\end{document}

enter image description here

  • Thank you @samcarter, your answer is always like a bolt:) – Eric Sun Oct 26 '18 at 13:49
  • check the definition of \baselineskip, one follow up question is, why 0.8, not 1? – Eric Sun Oct 26 '18 at 13:55
  • @EricSun I used 0.8 instead of 1 to compensate for your change in font size from normal to tiny. (but the number is just a guess, you could look up the exact difference) – user36296 Oct 26 '18 at 14:12
  • get it, 0.8 works perfectly in this case. – Eric Sun Oct 26 '18 at 14:14

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.