block 序号不见了

发布于 2022-12-14 16:33:47

请问这个主题下怎么将代码块左侧的序号调出来?

\documentclass{beamer}
\mode<presentation> {
    \usetheme{CambridgeUS}
}
\usepackage{listings}
\begin{document}
\begin{frame}[fragile]
    %    \frametitle{用Lingo求解LP}
    \begin{block}{Lingo代码}
        \begin{lstlisting}
model:  
max=50*x1+100*x2;
x1+x2<=300;
2*x1+x2<=400;
x2<=250;
x1>=0;
x2>=0;
end
\end{lstlisting}
    \end{block}
\end{frame}
\end{document}

ff796570dd30df33cfb426560ee0c290.png

查看更多

关注者
0
被浏览
800
3 个回答
LaTeXStudio
LaTeXStudio 2022-12-14
这家伙很懒,什么也没写!

要进行设置的,下面给个例子,代码来自这里:https://www.latexstudio.net/index/details/index/mid/755.html

\documentclass{beamer}
\mode<presentation> {
    \usetheme{CambridgeUS}
}
\usepackage{listings,color}

% Matlab highlight color settings
%\definecolor{mBasic}{RGB}{248,248,242}       % default
\definecolor{mKeyword}{RGB}{0,0,255}          % bule
\definecolor{mString}{RGB}{160,32,240}        % purple
\definecolor{mComment}{RGB}{34,139,34}        % green
\definecolor{mBackground}{RGB}{245,245,245}   % lightgrey
\definecolor{mNumber}{RGB}{134,145,148}       % gray

\definecolor{Numberbg}{RGB}{237,240,241}     % lightgrey

% Python highlight color settings
%\definecolor{pBasic}{RGB}{248, 248, 242}     % default
\definecolor{pKeyword}{RGB}{228,0,128}        % magenta
\definecolor{pString}{RGB}{148,0,209}         % purple
\definecolor{pComment}{RGB}{117,113,94}       % gray
\definecolor{pIdentifier}{RGB}{166, 226, 46}  %
\definecolor{pBackground}{RGB}{245,245,245}   % lightgrey
\definecolor{pNumber}{RGB}{134,145,148}       % gray


\lstset{language=Matlab,               % choose the language of the code
  %frame=tlbr,
  xleftmargin=30pt,
  xrightmargin=10pt,
  frame=l,
  framesep=15pt,%framerule=0pt,  % sets the frame style
  %frame=shadowbox,rulesepcolor=\color{red!20!green!20!blue!20},
  basicstyle=\small\ttfamily,
  keywordstyle={\color{mKeyword}},     % sets color for keywords
  stringstyle={\color{mString}},       % sets color for strings
  commentstyle={\color{mComment}},     % sets color for comments
  backgroundcolor=\color{mBackground}, % choose the background color 
  keywords={break,case,catch,classdef,continue,else,elseif,end,for,
  function,global,if,otherwise,parfor,persistent,return,spmd,switch,try,while},
  showspaces=false,                    % show spaces adding particular underscores
  showstringspaces=false,              % underline spaces within strings
  showtabs=false,                      % show tabs within strings adding particular underscores
  tabsize=4,                           % sets default tabsize to 2 spaces
  captionpos=t,                        % sets the caption-position to bottom
  breaklines=true,                     % sets automatic line breaking
  framexleftmargin=5pt,
  fillcolor=\color{Numberbg},
  rulecolor=\color{Numberbg},
  numberstyle=\tiny\color{mNumber},
  numbersep=9pt,                      % how far the line-numbers are from the code
  numbers=left,                        % where to put the line-numbers
  stepnumber=1,                        % the step between two line-numbers.
}  
\begin{document}
\begin{frame}[fragile]
    %    \frametitle{用Lingo求解LP}
    \begin{block}{Lingo代码}
        \begin{lstlisting}
model:
max=50*x1+100*x2;
x1+x2<=300;
2*x1+x2<=400;
x2<=250;
x1>=0;
x2>=0;
end
\end{lstlisting}
    \end{block}
\end{frame}
\end{document}

image.png

乙醇
乙醇 2022-12-14
这家伙很懒,什么也没写!

并不是序号不见了, 是 listings 宏包需要进行设置 numbers=left, 另外参考下面这个回答:

https://tex.stackexchange.com/a/466031/180617

可以知道是 CambridgeUS 这个主题的 leftmargin 太小, 更改后的代码如下:

\documentclass{beamer}
\mode<presentation> {
    \usetheme{CambridgeUS}
}
\usepackage{listings}
\lstset{
  numbers=left,
  xleftmargin=0.5cm
}
\begin{document}
\begin{frame}[fragile]
    %    \frametitle{用Lingo求解LP}
    \begin{block}{Lingo}
        \begin{lstlisting}
model:  
max=50*x1+100*x2;
x1+x2<=300;
2*x1+x2<=400;
x2<=250;
x1>=0;
x2>=0;
end
\end{lstlisting}
    \end{block}
\end{frame}
\end{document}

image.png

蒋蒋
蒋蒋 2022-12-14
这家伙很懒,什么也没写!

谢谢啊

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览