我怎样才能将以下自定义新环境放置在每个页面的顶部,就像图形(选项[ht])一样?
\usepackage[newfloat]{minted}
\newenvironment{code}{\captionsetup{type=listing}}{}
\SetupFloatingEnvironment{listing}{name=Listing}
\begin{document}
\begin{code}
\begin{minted}{python}
some code
\end{minted}
\caption{Example caption}
\label{lst:label}
\end{code}
\end{document}
我的文档中的插入是通过放置在没有任何边距的书写环境中来实现的,我该如何添加它们并改善页面的布局?
编辑:例如,在此页面中,您可以看到自定义环境中的代码位于底部
我需要将定位设置为页面顶部,就像一个图
答案1
使用该包,newfloat
您可以定义一个位于顶部的新浮点数。
\documentclass{article}
\usepackage{kantlipsum}% dummy tex
\usepackage{newfloat} % added <<<<<
\DeclareFloatingEnvironment[placement={!t}]{Listing} % new float <<<<
\begin{document}
1. \kant[1]
\begin{Listing}
2. \kant[2] % # 2 goes to the top of the page
\end{Listing}
3. \kant[3]
\end{document}
更新使用新发布的 MWE。
\documentclass{article}
\usepackage{kantlipsum}% dummy tex
\usepackage[newfloat]{minted}
\DeclareFloatingEnvironment[placement={!t}]{code} % new float <<<<
\begin{document}
1. \kant[1]
\begin{code}
\begin{minted}{python}
# Solve the quadratic equation ax**2 + bx + c = 0
# import complex math module
import cmath
a = 24
b = 4
c = 46
# calculate the discriminant
d = (b**2) - (4*a*c)
# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
\end{minted}
\caption{Example caption}
\label{lst:label}
\end{code}
\end{document}
根据具体情况来控制浮子的位置。
\documentclass{article}
\usepackage{kantlipsum}% dummy tex
\usepackage[newfloat]{minted}
\SetupFloatingEnvironment{listing}{name=Code}
\begin{document}
1. \kant[1]
\begin{listing}[!t] % control the placements case by case <<<<<
\begin{minted}{python}
# Solve the quadratic equation ax**2 + bx + c = 0
# import complex math module
import cmath
a = 24
b = 4
c = 46
# calculate the discriminant
d = (b**2) - (4*a*c)
# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
\end{minted}
\caption{Example caption}
\label{lst:label}
\end{listing}
\end{document}