我正在尝试使用tikzpicture
。tcblisting
基本上,我试图设计一个图形来展示Java编译器的使用,它将Java源代码转换为字节码。
请参阅下面的代码片段-
\documentclass{standalone}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows}
\begin{document}
\begin{tcblisting}{
title=Source Code,
hbox,
listing only,
listing options={
language=Java,
basicstyle=\ttfamily,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
commentstyle=\color{green}\ttfamily,
morecomment={[l][\color{magenta}]{\#}},
}
}
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}
}
\end{tcblisting}
\begin{tikzpicture}
\node[single arrow, draw, fill=red!40, text centered, text width=2cm, minimum width=2cm, font=\large] {Java Compiler};
\end{tikzpicture}
\begin{tcblisting}{
title=Bytecode,
hbox,
listing only,
listing options={
language=C++,
basicstyle=\ttfamily,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
commentstyle=\color{green}\ttfamily,
morecomment={[l][\color{magenta}]{\#}},
}
}
HelloWord.class
\end{tcblisting}
\end{document}
我想保持tcblisting
垂直顶部对齐和tikzpicture
垂直中心对齐。
答案1
tcolorboxes
多或少tikzpictures
,因此,它们可以是remembered as
并被引用为其他TikZ nodes
。
以下解决方案使用 Torbjørn T. 代码并enhanced, remember as = ...
为每个添加选项tcolorbox
。它们以一定距离绘制,然后arrow
使用库根据此距离进行调整calc
。
\documentclass{standalone}
\usepackage[most]{tcolorbox} %<---- Added [most] option
\tcbuselibrary{listings}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows, calc} %<--- Added `calc` library
\begin{document}
\begin{tcblisting}{
title=Source Code,
enhanced, % <-- added
remember as = source, % <-- added
box align=top,
hbox,
listing only,
listing options={
language=Java,
basicstyle=\ttfamily,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
commentstyle=\color{green}\ttfamily,
morecomment={[l][\color{magenta}]{\#}},
}
}
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}
}
\end{tcblisting}
\hspace{3cm} %<---- Fix the space between boxes
\begin{tcblisting}{
title=Bytecode,
enhanced, % <-- added
remember as=bytecode, % <-- added
box align=top,
hbox,
listing only,
listing options={
language=C++,
basicstyle=\ttfamily,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
commentstyle=\color{green}\ttfamily,
morecomment={[l][\color{magenta}]{\#}},
}
}
HelloWord.class
\end{tcblisting}
\begin{tikzpicture}[remember picture, overlay]
% added `let` construction to compute minimum height
% added `anchor` and positioning coordinate
\path let \p1=($(bytecode.north west)-(source.north east)$) in
node[single arrow, draw, fill=red!40, text centered,
text width=18mm, minimum height={veclen(\x1,\y1)},
font=\large, anchor=after tail] (arr) at
([yshift=-5mm]source.north east) {Java Compiler};
\end{tikzpicture}
\end{document}
答案2
添加box align=top
以将两个tcolorbox
es 对齐到顶部,然后将baseline
的键设置tikzpicture
为有用的内容,请参阅代码。使用 ,baseline=(arr.before tip)
您将获得屏幕截图中的输出。arr
是我给箭头节点起的名字。如果您想以不同的方式定位它,您可以使用长度(例如baseline=2cm
),甚至像 这样的内容baseline={([yshift=5mm]arr.before tip)}
。
\documentclass{standalone}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows}
\begin{document}
\begin{tcblisting}{
title=Source Code,
box align=top, % <-- added
hbox,
listing only,
listing options={
language=Java,
basicstyle=\ttfamily,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
commentstyle=\color{green}\ttfamily,
morecomment={[l][\color{magenta}]{\#}},
}
}
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}
}
\end{tcblisting}
\begin{tikzpicture}[
baseline=(arr.before tip)
% or you can use a specific length, adjust to your liking
% baseline=1cm
]
% added (arr) to name the node
\node[single arrow, draw, fill=red!40, text centered, text width=2cm, minimum width=2cm, font=\large] (arr) {Java Compiler};
\end{tikzpicture}
\begin{tcblisting}{
title=Bytecode,
box align=top, % <-- added
hbox,
listing only,
listing options={
language=C++,
basicstyle=\ttfamily,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
commentstyle=\color{green}\ttfamily,
morecomment={[l][\color{magenta}]{\#}},
}
}
HelloWord.class
\end{tcblisting}
\end{document}