I have the following Latex
environment:
\newcounter{txtctr开发者_Go百科}[section] \setcounter{txtctr}{0}
\newenvironment{textex}{% This is the begin code
\refstepcounter{txtctr} \vspace{0.2cm} {\noindent
(T.\arabic{chapter}.\arabic{section}.\arabic{txtctr}):
}\footnotesize\vspace{-0.2cm}}{\normalsize}
but I would like to have the counter to the right and in the middle of the text within the environment. Something like:
Canis per flumen carnem dum ferret natans
aliamque praedam ab alio (T1.1.1)
Canis per flumen carnem dum ferret natans
I suppose this may have to be done with a minipage
? By the way, the environment must be friendly to the verbatim
environment.
Could some Latex wizard please help me?
Thank you.
I've got a suspicion that there's a package to do this automatically (i.e., numbered code examples), but nothing comes to mind right now.
Which verbatim package(s) you're using dictates exactly how you want to do this, but the general technique for vertically centring boxes like this is to use minipage as you suggest. Just set their widths appropriately and there's not much more to it.
\begin{minipage}[c]{0.9\linewidth} % your environment goes here \end{minipage}\hfill \begin{minipage}[c]{0.09\linewidth} (T.\arabic{chapter}.\arabic{section}.\arabic{txtctr})% \end{minipage}
try and tune up this:
\documentclass{article}
\newcounter{txtctr}%Defines couter
\def\thetxtctr{\thesection-\arabic{txtctr}}%Typesets counter
\newenvironment{textex}{% This is the begin code
\begin{minipage}[c]{.8\textwidth}%
}%
{% This is ending code
\end{minipage}
\begin{minipage}[c]{.2\textwidth}
\flushright\thetxtctr
\end{minipage}
}
\begin{document}
\section{hhh}
\begin{textex}
\begin{verbatim}
Canis per flumen carnem dum ferret natans
aliamque praedam ab alio
Canis per flumen carnem dum ferret natans
\end{verbatim}
\end{textex}
\end{document}
See answer of Alexey Malistov
精彩评论