TransWikia.com

listings package und set lstinline style?

TeX - LaTeX Asked by Ameyah on January 31, 2021

I set 3 styles in my lecture notes for 3 different code style enviroments (lstdefinestyle and lstnewenvironment).
How can I set the lstinline style to one of these? Or is it not possible?

Because the lstinline has very small font now and is not uniform with the normal text. (the documentation doesn’t really help there? :/)

Here is an example code

documentclass[12pt,twoside,a4paper,fleqn,ngerman]{report} %Koma-Script Class article

RequirePackage{listings}               % Programmierprachen
letllstinline
letttext
RequirePackage{lmodern}                % customized font size 
RequirePackage{color}
definecolor{mygray}{rgb}{0.31,0.31,0.31}

% style for appendix with smaller size for the appendix with custom size 9pt
lstdefinestyle{small_customf95}{
    % belowcaptionskip=1baselineskip,
    breaklines=true,
    frame=none,
    xleftmargin=0.5cm,
    language=[95]Fortran,
    showstringspaces=false,
    basicstyle=footnotesizettfamily,
    keywordstyle=bfseries,
    commentstyle=color{mygray},
    % identifierstyle=color{blue},
    % stringstyle=color{orange},
}
lstset{mathescape=true,escapeinside={@}{@},style=small_customf95}

lstnewenvironment{smallf95}{%
    lstset{%
        mathescape=true,escapeinside={@}{@},style=small_customf95,
    }
}{}

% style for appendix with smaller size for the appendix with custom size 9pt, very small !!! only use
%if the other size is still to big !!!
lstdefinestyle{smaller_customf95}{
    % belowcaptionskip=1baselineskip,
    breaklines=true,
    frame=none,
    xleftmargin=0.5cm,
    language=[95]Fortran,
    showstringspaces=false,
    basicstyle=fontsize{9pt}{9pt}ttfamily,
    keywordstyle=bfseries,
    commentstyle=color{mygray},
    % identifierstyle=color{blue},
    % stringstyle=color{orange},
}
lstset{mathescape=true,escapeinside={@}{@},style=smaller_customf95}

lstnewenvironment{smallerf95}{%
    lstset{%
        mathescape=true,escapeinside={@}{@},style=smaller_customf95,
    }
}{}

%default style for F95 code for the actual lectures, size 8pt
lstdefinestyle{customf95}{
    % belowcaptionskip=1baselineskip,
    breaklines=true,
    frame=none,
    xleftmargin=0.5cm,
    language=[95]Fortran,
    showstringspaces=false,
    basicstyle=smallttfamily,
    keywordstyle=bfseries,
    commentstyle=color{mygray},
    % identifierstyle=color{blue},
    % stringstyle=color{orange},
}
%lstset{mathescape=true,escapeinside={@}{@},style=customf95}
lstnewenvironment{basicf95}{%
    lstset{%
        mathescape=true,escapeinside={@}{@},style=customf95
    }
}{}

begin{document}
    test l{ADVANCE=?}.
begin{basicf95}
    PROGRAM test
        IF blabla
            ...
        END IF
    END PROGRAM test
end{basicf95}
l{ADVANCE=?} - now?
end{document}

2 Answers

The manual says, that lstinline takes an optional argument, where you can set the style:

test lstinline[style=customf95]{ADVANCE=?}.

enter image description here

Also, if you need short inline code, you can define

lstMakeShortInline[style=customf95]|

Which can be used like shortvrb

test |ADVANCE=?|.

Answered by DG' on January 31, 2021

Just define one style and make the suitable adjustments when defining the environments: a change in the overall style will be reflected also in all three environments and in the inline version.

documentclass[12pt,twoside,a4paper,fleqn,ngerman]{report} %Koma-Script Class article
usepackage[T1]{fontenc}
usepackage{babel}

usepackage{listings}               % Programmierprachen
usepackage{lmodern}                % customized font size 
usepackage{color}

definecolor{mygray}{rgb}{0.31,0.31,0.31}

% style for appendix with smaller size for the appendix with custom size 9pt

lstdefinestyle{customf95}{
    % belowcaptionskip=1baselineskip,
    breaklines=true,
    frame=none,
    xleftmargin=0.5cm,
    language=[95]Fortran,
    showstringspaces=false,
    keywordstyle=bfseries,
    commentstyle=color{mygray},
    % identifierstyle=color{blue},
    % stringstyle=color{orange},
    mathescape=true,
    escapeinside={@}{@},
}

lstnewenvironment{basicf95}
 {%
  lstset{
    style=customf95,
    basicstyle=smallttfamily,
  }%
 }{}

lstnewenvironment{smallf95}
 {%
  lstset{
    style=customf95,
    basicstyle=footnotesizettfamily,
  }%
 }{}
lstnewenvironment{smallerf95}
 {%
  lstset{
    style=customf95,
    basicstyle=fontsize{9pt}{11pt}ttfamily,
  }%
 }{}
newcommand{inlinef}[1][]{%
  lstinline[style=customf95,basicstyle=ttfamily,#1]%
}


begin{document}

test inlinef{ADVANCE=?}.

begin{basicf95}
    PROGRAM test
        IF blabla
            ...
        END IF
    END PROGRAM test
end{basicf95}

inlinef{ADVANCE=?} - now?

begin{smallerf95}
    PROGRAM test
        IF blabla
            ...
        END IF
    END PROGRAM test
end{smallerf95}

inlinef{ADVANCE=?} - now?

begin{smallf95}
    PROGRAM test
        IF blabla
            ...
        END IF
    END PROGRAM test
end{smallf95}

inlinef{ADVANCE=?} - now?

end{document}

Avoid redefine l and t: when your bibliography will contain the name of some Polish author you'd regret having redefined l.

enter image description here

Answered by egreg on January 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP