3

How can I create two boxes in the same line with words in between(as the picture below which I made using MS-Word)? I tried \makebox, \fbox or \minipage and none of them seem to be working, maybe because I am using verbatim environment at the same time. I am trying to make a section illustrating how to type Chinese in LaTeX.

I have tried

\fbox{
\begin{minipage}{5cm}
\begin{verbatim}
\documentclass{article}
\usepackage[UTF8]{ctex}
\end{verbatim}  
\end{minipage}
}

or any other possible combination of this three command but the typeset engine always give me errors says there is one extra { at the end of the code, even though it should be the right bracket of \fbox. The effect I want

  • 1
    tcolorbox and its listings library can make such code boxes with basically any fancy decoration. – user31729 Mar 22 '17 at 20:07
  • @Christian supplied you with a suggestion on how to solve your problem, however, if you want more extensive help with your question it would help if you could supply a MWE with what you have tried so far and to give us a starting point. – Timm Mar 22 '17 at 20:37
  • @Timm Thank you very much. I have already editted my question. – pkqxdd Mar 22 '17 at 20:44
  • The problem isn't two boxes in the same line, it's putting verbatim inside a box. – John Kormylo Mar 22 '17 at 22:03
4

For an easy-to-use application, just set these in a tabular:

enter image description here

\documentclass{article}

\begin{document}

Either
\begin{tabular}{ | @{\,} l @{\,} | }
  \hline
  \verb|\documentclass{article}| \\
  \verb|\usepackage[UTF8]{ctex}| \\
  \hline
\end{tabular}
or
\begin{tabular}{ | @{\,} l @{\,} | }
  \hline
  \verb|\documentclass[UTF8]{ctexart}| \\
  \verb|\begin{document}| \\
  \verb|\end{document}| \\
  \hline
\end{tabular}.

\end{document}

Another option using fancyvrb to save the verbatim content and use it inside \fbox:

enter image description here

\documentclass{article}

\usepackage{fancyvrb}

\begin{document}

\begin{SaveVerbatim}{optA}
\documentclass{article}
\usepackage[UTF8]{ctex}
\end{SaveVerbatim}
\begin{SaveVerbatim}{optB}
\documentclass[UTF8]{ctexart}
\begin{document}
\end{document}
\end{SaveVerbatim}

Either \fbox{\strut\BUseVerbatim{optA}} or \fbox{\strut\BUseVerbatim{optB}}.

\end{document}

\struts ensure a consistent baseline with respect to the two verbatims.

7

With a tcblisting from tcolorbox:

\documentclass[11pt,a4paper]{article}

\usepackage[most]{tcolorbox}

\usepackage{lipsum}

\newtcblisting{mybox}[2][]{%
    nobeforeafter, listing only,
    box align=center,
    sharp corners, width=#2, notitle, size=fbox, #1}

\begin{document}

Either 
\begin{mybox}[after=\ ]{5cm}
\documentclass{article}
\usepackage[UTF8]{ctex}
\end{mybox} 
or 
\begin{mybox}[colback=white]{6cm}
\documentclass[UTF8]{ctexart}
\begin{document}
\end{document}
\end{mybox}
\end{document}

enter image description here

  • The background colour of the right box hurts my eyes ;-) – user31729 Mar 22 '17 at 21:12
  • @ChristianHupfer Don't worry, it's easy to change. I hope you like it now. ;-) I've also improved the distance between first box and text. – Ignasi Mar 23 '17 at 7:55
  • I know how to change the background colour of a tcolorbox ;-) Yes, it's better now :D – user31729 Mar 23 '17 at 15:15
  • this would look nicer with not as much blank space at the right side of the boxes, and slightly larger spaces around "or", – barbara beeton Mar 24 '17 at 15:55
4

You can use fancyvrb facilities. Note that the environment's contents should be typed at the left margin, due to the properties of verbatim.

Disadvantage: you have to adjust the spacing following the environment.

Advantage: you don't have to guess the width.

\documentclass{article}

\usepackage{fancyvrb}

\newenvironment{FBVerbatim}
  {\VerbatimEnvironment
   \begin{lrbox}{\FBVerbatimbox}
   \begin{BVerbatim}}
  {\end{BVerbatim}
   \end{lrbox}
   \fbox{\begin{tabular}{@{}c@{}}\usebox{\FBVerbatimbox}\end{tabular}}}

\newsavebox{\FBVerbatimbox}

\begin{document}

Either
\begin{FBVerbatim}
\documentclass{article}
\usepackage[UTF8]{ctex}
\end{FBVerbatim}
\ or
\begin{FBVerbatim}
\documentclass[UTF8]{ctexart}
\begin{document}
\end{document}
\end{FBVerbatim}
\,.

\end{document}

enter image description here

1

Interestingly, \hbox worked (but not \savebox).

\documentclass{article}

\begin{document}
\setbox0=\hbox{\begin{minipage}{5cm}
\begin{verbatim}
\documentclass{article}
\usepackage[UTF8]{ctex}
\end{verbatim}  
\end{minipage}}%
\fbox{\usebox0}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.