漂浮 類似框架

漂浮 類似框架

我對 LaTeX 的經驗很少。那麼,請原諒我的無知,但是是否有一個相當於 的 float \framebox

理想情況下,我想要一個簡單的框架文字結構,可以將其放置在頁面的頂部 [t] 或底部 [b]。

我發現兩個帖子似乎並不能完全滿足我的需求:

  1. 帶有浮動的框架文本
  2. 文字和圖形周圍的框架

答案1

tcolorbox這是創建浮動框的另一種方法。

第一個版本是一種簡約的方法:

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}

\newtcolorbox{framefloat}[1][!tb]{arc=0pt,outer arc=0pt,boxrule=0.4pt,
  colframe=black,colback=white,float=#1}

\begin{document}

\begin{framefloat}
\textbf{Box on top:}
\lipsum[2]
\end{framefloat}

\begin{framefloat}[b]
\textbf{Box on bottom:}
\lipsum[2]
\end{framefloat}

\lipsum[1-3]

\end{document}

在此輸入影像描述

tcolorbox如果您正在尋找類似的東西,則可以使用更多選項來創建更精美的風格。第二個版本使用一些顏色選項並交換選項參數以採用逗號分隔的選項清單。此float選項採用已知的浮動參數。

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}

\newtcolorbox{framefloat}[1][]{fonttitle=\bfseries,
  colframe=yellow!30!black,colback=yellow!10!white,float=!tb,#1}

\begin{document}

\begin{framefloat}[title=Box on top]
\lipsum[2]
\end{framefloat}

\begin{framefloat}[title=Box on bottom,float=b]
\lipsum[2]
\end{framefloat}

\lipsum[1-3]

\end{document}

在此輸入影像描述

答案2

\documentclass{scrartcl}
\usepackage{varwidth}
\newsavebox\FBox
\newenvironment{framefloat}[1][!htb]
  {\begin{table}[#1]\centering\begin{lrbox}{\FBox}
   \varwidth{\dimexpr\linewidth-2\fboxsep-2\fboxrule}}
  {\endvarwidth\end{lrbox}\fbox{\usebox\FBox}\end{table}}

\usepackage{blindtext}

\begin{document}

\blindtext

\begin{framefloat}[t]
\blindtext
\end{framefloat}

\blindtext

\begin{framefloat}
\blindtext
\end{framefloat}

\end{document}

在此輸入影像描述

答案3

這是一個使用以下組合的選項mdframed,newfloat, 和xparse包(由 加載mdframed)。

以下行:

\DeclareFloatingEnvironment[placement={!ht}]{myfloat}

聲明一個名為 的新浮動環境myfloat,其中有預設和的放置!ht可以被覆蓋。

以下幾行創建了一個新的浮動環境,它myfloat與套件中的環境組合在一起mdframed

\NewDocumentEnvironment{framefloat}{O{}O{}}
    {% #1: float position (optional)
     % #2: options for mdframed (optional)
     \begin{myfloat}[#1]
    \begin{mdframed}[roundcorner=10pt,backgroundcolor=green,#2]
    }
    {\end{mdframed}\end{myfloat}
    }

正如您所看到的,它使用套件中的語法xparse來聲明兩個可選參數:

    % #1: float position (optional)
    % #2: options for mdframed (optional)

它可以透過以下任何一種方式使用(例如):

\begin{framefloat}

\begin{framefloat}[t]

\begin{framefloat}[b][backgroundcolor=blue]

\begin{framefloat}[][backgroundcolor=red]

這是一個完整的 MWE 可供使用。我用過的每個軟體包都有很多更多功能;探索您認為合適的方式。

% arara: pdflatex
\documentclass{article}
\usepackage{newfloat}
\usepackage{lipsum}
\usepackage[framemethod=TikZ]{mdframed}

% new float
\DeclareFloatingEnvironment[placement={!ht}]{myfloat}

% new floating framed environment
\NewDocumentEnvironment{framefloat}{O{}O{}}
    {\begin{myfloat}[#1]
    \begin{mdframed}[roundcorner=10pt,backgroundcolor=green,#2]
    }
    {\end{mdframed}\end{myfloat}
    }

\begin{document}

\lipsum

\begin{framefloat}
\lipsum[1]
\end{framefloat}

\lipsum

\begin{framefloat}[t]
\lipsum[1]
\end{framefloat}

\lipsum

\begin{framefloat}[b][backgroundcolor=blue]
\lipsum[1]
\end{framefloat}

\lipsum

\begin{framefloat}[][backgroundcolor=red]
\lipsum[1]
\end{framefloat}


\end{document} 

相關內容