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

share|improve this question
1  
tcolorbox and its listings library can make such code boxes with basically any fancy decoration. – Christian Hupfer 10 hours ago
    
@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 10 hours ago
    
@Timm Thank you very much. I have already editted my question. – pkqxdd 9 hours ago
    
The problem isn't two boxes in the same line, it's putting verbatim inside a box. – John Kormylo 8 hours ago
up vote 3 down vote accepted

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.

share|improve this answer

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, #1}

\begin{document}

Either 
\begin{mybox}{5cm}
\documentclass{article}
\usepackage[UTF8]{ctex}
\end{mybox}
or
\begin{mybox}[colback=red!30]{7cm}
\documentclass[UTF8]{ctexart}
\begin{document}
\end{document}
\end{mybox}
\end{document}

enter image description here

share|improve this answer
    
The background colour of the right box hurts my eyes ;-) – Christian Hupfer 9 hours ago

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

share|improve this answer

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}
share|improve this answer
    
But lrbox works ;-). However I think you mean setbox vs savebox tex.stackexchange.com/questions/46480/… – Marco Daniel 8 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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