0

I have below lines of code and I want to add it in my beamer presentation. I tried below mentioned approach but it failed. The requirement is that it should also show line number.


1. int main(){
2. printf("hello");
3. return 0;

}

I Used this:

\begin{frame}[fragile]
    \semiverbatim{
            1. int main(){
            2. printf("hello");
            3. return 0;
} 

\end{frame}

shyam
  • 3
  • 1
    also "it failed" is not anything that anyone can debug, If you got an error show the exact error message that you got, copied from the log filre as text. – David Carlisle Sep 19 '21 at 18:52
  • 1
    One common issue is that you need to make a frame fragile in order to include any verbatim content such as listings, i.e., \begin{frame}[fragile]. – Marijn Sep 19 '21 at 18:52
  • When you say 'I tried the below mentioned approach', do you mean the answer by tobiasBora? If so, then how exactly did it fail? From his screenshot you can see that it works for him, what is different for you? Or did you mean you tried my comment above? – Marijn Sep 19 '21 at 19:00

1 Answers1

1

You need a \begin{frame}[fragile] environment, for line numbering you can use the listings package, it has numerous options, for line numbering it is numbers=left. You can also use \lstinputlisting{yourfile.c} to input directly a C file, and lstautogobble is useful to remove indentation based on the first line. For more options (colors…) to typeset C code, see e.g. this answer, I provided a very simple example. Btw, next time provide a full (i.e. compilable) minimal working example like this:

enter image description here

\documentclass[]{beamer}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{lstautogobble} % Provides autogobble, which is useful to remove indentation based on first line

\lstset{ % language=C, numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt, breaklines=true, autogobble=true, % Removes indentation based on first line }

\begin{document}

\begin{frame}[fragile]{Title} \begin{center} \begin{tabular}{c} \begin{lstlisting} int main(){ printf("hello"); return 0; } \end{lstlisting} \end{tabular} \end{center} \end{frame}

\end{document}

tobiasBora
  • 8,684
  • It looks good but numbers on left is not coming – shyam Sep 19 '21 at 19:09
  • @shyam Really? That's strange. Maybe try to compile several times. Also, I tried to add a few more options, I'm not sure if it will change something but you can try. If it's not enough, you can edit your answer with a screenshot of what you obtain, and the code you use (it should be basically a copy/paste of what I sent). – tobiasBora Sep 19 '21 at 19:16
  • Yeah now it came. But i want to align to center. – shyam Sep 20 '21 at 04:14
  • @shyam Then a quick research would point you to this solution https://tex.stackexchange.com/questions/5818/how-to-center-a-listing : wrap your code inside \begin{center}\begin{tabular}{c} ... \end{tabular}\end{center}, – tobiasBora Sep 20 '21 at 08:03
  • I edited the code to automatically remove indentation based on first line (otherwise your code is shifted to the right) – tobiasBora Sep 20 '21 at 08:21
  • Thanks I understood it. – shyam Sep 20 '21 at 10:35