在下面的代码中,我希望能够将图像置于标题文本的中心,如下所示,其中图像的中心与文本的中心对齐:
这是我目前拥有的代码(包括来自哈普弗博士和谢泼德]:
\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}
\usepackage[no-math]{fontspec}
\usepackage{polyglossia}
\setdefaultlanguage{french}
\setotherlanguages{english}
\newcommand{\codeimg}{\includegraphics[scale=0.1]{example-image-a}}
\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{mdframed}
\usepackage{listings}
\usepackage{filecontents}
\definecolor{light-gray}{gray}{0.92}
\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red
\renewcommand\lstlistingname{Code}
\lstset{
language=Python,
numbers=left,
numbersep= 7mm,
numberstyle=\color{Black},
stepnumber=1,
tabsize=3,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
basicstyle=\color{Black}\ttfamily,
commentstyle=\color{LimeGreen},
keywordstyle=\color{BurntOrange}\bfseries,
stringstyle=\color{WildStrawberry},
keywords={var, func, extends},
frame=leftline,
framesep=0mm,
xleftmargin=3mm,% marge ajouté à gauche du tableau (à configurer en dernier pour l'alignement global du tableau)
framesep=2mm, %distance texte bord du cadre (limite de la background color)
framerule=0mm,
abovecaptionskip=5mm,
aboveskip=\baselineskip,
belowskip=\baselineskip
}
\usepackage{tcolorbox}
\newcounter{data}
\newcounter{result}
\newcounter{pythoncode}
\newcounter{matlab}
\tcbuselibrary{skins,breakable,listings}
\newtcblisting[use counter=lstlisting]{codeblock}[2][]{%
enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
fonttitle=\bfseries,before upper={\hspace*{-1em}\includegraphics[height=\baselineskip]{example-image-a}~#2},%
title after break={\centering\footnotesize\itshape\strut\lstlistingname~\thelstlisting~--~continued},%
listing only,listing options={xleftmargin=-1mm},
after upper={\centering\strut\lstlistingname~\thelstlisting:~#2},
frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,frame code={\draw[gray,line width=2mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},#1}
\newtcbinputlisting[use counter=matlab,list inside=matlab,number within=chapter]{\inputmatlab}[3][]{listing options={style=Matlab-editor},%
enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
fonttitle=\bfseries,before upper={\hspace*{-1em} \includegraphics[height=\baselineskip]{example-image-a}~#2},%
title after break={\centering\footnotesize\itshape\strut Matlab Code~\thematlab~--~continued},%
listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor},
after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,
%
listing file={#3},#1}
\usepackage[numbered,framed]{matlab-prettifier}
\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}
\begin{document}
\begin{codeblock}{Les bases de GDScript}
var nombreDeGardes = 4 #déclaration d'une variable
nombreDeGardes = plusDeux(nombreDeGardes) #appel d'une fonction avec la variable nombreDeGardes passée en paramètre
func maFonction(): #cette ligne déclare une nouvelle fonction nommée maFonction
var variable1 = 7 #nouvelle variable dont la valeur est 7
variable2 = ['un', 'deux', 'trois'] #variable assignée à un tableau contenant trois chaines de caractères
func plusDeux(argument1):
return argument1 + 2
\end{codeblock}
\inputmatlab{Expansion Algorithm}{sample.m}
\end{document}
答案1
有很多选项可以使图像垂直居中,您可以使用parbox
、、tabular
来自valign=m
adjustbox 包的,或者简单地使用\vcenter
数学模式中的宏,如下所示。
\newcommand{\codeimg}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-a}}}$}
这是您需要在下面的列表中插入的唯一命令。
\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}
\usepackage[no-math]{fontspec}
\usepackage{polyglossia}
\setdefaultlanguage{french}
\setotherlanguages{english}
\newcommand{\codeimg}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-a}}}$}
\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{mdframed}
\usepackage{listings}
\usepackage{filecontents}
\definecolor{light-gray}{gray}{0.92}
\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red
\renewcommand\lstlistingname{Code}
\lstset{
language=Python,
numbers=left,
numbersep= 7mm,
numberstyle=\color{Black},
stepnumber=1,
tabsize=3,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
basicstyle=\color{Black}\ttfamily,
commentstyle=\color{LimeGreen},
keywordstyle=\color{BurntOrange}\bfseries,
stringstyle=\color{WildStrawberry},
keywords={var, func, extends},
frame=leftline,
framesep=0mm,
xleftmargin=3mm,% marge ajouté à gauche du tableau (à configurer en dernier pour l'alignement global du tableau)
framesep=2mm, %distance texte bord du cadre (limite de la background color)
framerule=0mm,
abovecaptionskip=5mm,
aboveskip=\baselineskip,
belowskip=\baselineskip
}
\usepackage{tcolorbox}
\newcounter{data}
\newcounter{result}
\newcounter{pythoncode}
\newcounter{matlab}
\tcbuselibrary{skins,breakable,listings}
\newtcblisting[use counter=lstlisting]{codeblock}[2][]{%
enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimg~#2},%
title after break={\centering\footnotesize\itshape\strut\lstlistingname~\thelstlisting~--~continued},%
listing only,listing options={xleftmargin=-1mm},
after upper={\centering\strut\lstlistingname~\thelstlisting:~#2},
frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,frame code={\draw[gray,line width=2mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},#1}
\newtcbinputlisting[use counter=matlab,list inside=matlab,number within=chapter]{\inputmatlab}[3][]{listing options={style=Matlab-editor},%
enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimg~#2},%
title after break={\centering\footnotesize\itshape\strut Matlab Code~\thematlab~--~continued},%
listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor},
after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,
%
listing file={#3},#1}
\usepackage[numbered,framed]{matlab-prettifier}
\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}
\begin{document}
\begin{codeblock}{Les bases de GDScript}
var nombreDeGardes = 4 #déclaration d'une variable
nombreDeGardes = plusDeux(nombreDeGardes) #appel d'une fonction avec la variable nombreDeGardes passée en paramètre
func maFonction(): #cette ligne déclare une nouvelle fonction nommée maFonction
var variable1 = 7 #nouvelle variable dont la valeur est 7
variable2 = ['un', 'deux', 'trois'] #variable assignée à un tableau contenant trois chaines de caractères
func plusDeux(argument1):
return argument1 + 2
\end{codeblock}
\inputmatlab{Expansion Algorithm}{sample.m}
\end{document}