Mathematica:带有图像和文本的布局
Mathematica: Layout with Image and Text
感谢任何帮助!
我有很多图片和每张图片附带的文字。现在我想为每对图像和文本自动生成以下讲义:
- 图片在最上面。
- 文本应放在其下方,其样式应为
"justified" 并在可能的情况下启用连字符。
- 那么剩下的space应该是一个区域,标题名称:
"Notes",显然要记笔记。
如何使用 Mathematica 完成?
这里有一些图片可供尝试:
https://www.dropbox.com/sh/o8x992dz9gg7q9e/AAAyli1pO2f4q35ULNDreC1ua?dl=0
下面是可以搭配的示例文本:
text={{"As there are several possibilities to perform lithography,", "I will start by providing some definitions", "and by explaining some general concepts that are recurring for all", "the lithography variations.", "Then I will dive into details how one fabricates the lithography masks", "using a direct write laser tool."},{"Lithography is the fundamental process of transferring geometric shape", "from a design to a thin layer of radiation sensitive material", "called resist.", "Which is covering the surface of a wafer substrate.", "These shapes or patterns define the various regions", "in an integrated circuit, such as the implantation regions,", "the contact windows, the metallic wiring etc."},{"Each lithography follows a well defined series of process steps,","called process flow.", "It may vary according to the lithography used", "and the materials involved.", "But a typical generic example is shown here", "where we go step by step through it.", "First the substrate, it can be a silicon wafer or glass plate,"}}
这是我试过的:
Labeled[images[[#]], Framed[text[[#]]],
LabelStyle ->
Directive[Bold, Alignment -> TextJustification,
Hyphenation -> False, FontFamily -> "Helvetica"]] & /@
Range[Length[images]]
我不会做的事情:
- 添加额外的注释框
- A4 尺寸
- 使图片和文字大小合适
- 断字似乎也不能正常工作
这是一个方法。首先为您选择的输出设置打印区域。 PDF 边距可能不同于直接从 Mathematica 打印。请注意,由于设置 $FrontEndSession
,这些设置仅适用于当前会话。
SetOptions[$FrontEndSession, PrintingOptions -> {
"PrintingMargins" -> {
{10(* position left *), 40(* clip right *)},
{30(* clip bottom *), 25(* position top *)}},
"FirstPageHeader" -> False,
"FirstPageFooter" -> False,
"RestPagesHeader" -> False,
"RestPagesFooter" -> False,
"Magnification" -> 1}];
testpage = Graphics[{White, Rectangle[{0, 0}, {700, 1080}], Black,
Line[{{0, 0}, {700 - 1, 0}, {700 - 1, 1080}, {0, 1080}, {0, 0}}]},
PlotRange -> {{0, 700}, {0, 1080}}, ImageSize -> 700];
nb = CreateDocument[ExpressionCell[testpage, "Print"], WindowSize -> 850];
Export[FileNameJoin[{$InitialDirectory, "testpage.pdf"}], nb];
NotebookClose[nb]
使用 OP 的图像和文本之一,修改为包含用于换行演示的连字符。
image = Import[FileNameJoin[{$InitialDirectory, "p1.jpg"}]];
text1 = StringRiffle[StringReplace[StringRiffle[#, " "], {". " -> ". ",
"some definitions" -> "some-definitions"}] & /@ Most@text, "\n\n"];
页面创建功能。
createPage[{image_, text_, number_, pdfname_}] := Module[{y = -200},
page = Graphics[{White, Rectangle[{0, 0}, {700, 1080}], Black,
Inset[image, {0, 1080}, {Left, Top}, {700, Automatic}],
Inset[
Graphics[{Blue, Thickness[0.005],
Line[{{0, 600 + 1 + y}, {700 - 1, 600 + 1 + y},
{700 - 1, 800 + y}, {0, 800 + y}, {0, 0}}],
Black,
Inset[
TextCell[text, LineSpacing -> {0, 16}, TextJustification -> 1],
Center, Center, {600, Automatic}]
},
PlotRange -> {{0, 700}, {600 + y, 800 + y}}, ImageSize -> 700,
BaseStyle -> {FontFamily -> "Times",
FontWeight -> "Plain", FontSize -> 15.5}],
{0, 0}, {0, 0}, {700, Automatic}],
Inset[
Graphics[{
Red,
Line[{{570, 100 + 1}, {630 - 1, 100 + 1},
{630 - 1, 160}, {570, 160}, {570, 100 + 1}}],
Black,
Inset[ToString[number], Center, Center, Automatic]
},
PlotRange -> {{570, 630}, {100, 160}}, ImageSize -> 60,
BaseStyle -> {FontFamily -> "Times",
FontWeight -> "Plain", FontSize -> 32}],
{0, 0}, {0, 0}, {60, Automatic}]
},
PlotRange -> {{0, 700}, {0, 1080}}, ImageSize -> 700,
BaseStyle -> {FontFamily -> "Times",
FontWeight -> "Plain", FontSize -> 15.5}];
nb = CreateDocument[ExpressionCell[page, "Print"], WindowSize -> 850];
Export[FileNameJoin[{$InitialDirectory, pdfname}], nb];
NotebookClose[nb]]
createPage[{image, text1, 4, "page.pdf"}]
注意。 +1
和 -1
调整仅适用于屏幕渲染。在打印或转换为 PDF 时可以省略它们。
估计文本块高度
如果您的文本插入页的高度因页面而异,则可以使用此例程以编程方式调整布局。请注意,这是近似值,因为它在 "Working"
screen style environment 中运行,而 PDF 或打印使用略有不同的 "Printout"
环境。例如,第一段在 "Printout"
环境中换行到四行。
makebox[wd_, ht_] := Graphics[{Green, Rectangle[{0, 0}, {wd, ht}],
Black, Inset[
TextCell[text1, LineSpacing -> {0, 16}, TextJustification -> 1],
{0, 0}, {Left, Bottom}, {wd, Automatic}]},
BaseStyle -> {FontFamily -> "Times",
FontWeight -> "Plain", FontSize -> 15.5},
PlotRange -> {{0, wd}, {0, ht}}, ImageSize -> wd]
box = makebox[600, 900];
raster = Rasterize[box, "Data"];
topline = raster[[1]] = raster[[2]];
newheight = Length[Reverse@raster /. {x__, topline ..} :> {x}] + 4;
Row[{"Text block pixel height is ", newheight}]
makebox[600, newheight]
多页 PDF
在单个 PDF 中发布多个页面。
nb = CreateDocument[{
ExpressionCell[page1, "Print"],
ExpressionCell[page2, "Print"],
ExpressionCell[page3, "Print"]}, WindowSize -> 850];
Export[FileNameJoin[{$InitialDirectory, "allpages.pdf"}], nb];
NotebookClose[nb]
感谢任何帮助!
我有很多图片和每张图片附带的文字。现在我想为每对图像和文本自动生成以下讲义:
- 图片在最上面。
- 文本应放在其下方,其样式应为 "justified" 并在可能的情况下启用连字符。
- 那么剩下的space应该是一个区域,标题名称: "Notes",显然要记笔记。
如何使用 Mathematica 完成?
这里有一些图片可供尝试: https://www.dropbox.com/sh/o8x992dz9gg7q9e/AAAyli1pO2f4q35ULNDreC1ua?dl=0
下面是可以搭配的示例文本:
text={{"As there are several possibilities to perform lithography,", "I will start by providing some definitions", "and by explaining some general concepts that are recurring for all", "the lithography variations.", "Then I will dive into details how one fabricates the lithography masks", "using a direct write laser tool."},{"Lithography is the fundamental process of transferring geometric shape", "from a design to a thin layer of radiation sensitive material", "called resist.", "Which is covering the surface of a wafer substrate.", "These shapes or patterns define the various regions", "in an integrated circuit, such as the implantation regions,", "the contact windows, the metallic wiring etc."},{"Each lithography follows a well defined series of process steps,","called process flow.", "It may vary according to the lithography used", "and the materials involved.", "But a typical generic example is shown here", "where we go step by step through it.", "First the substrate, it can be a silicon wafer or glass plate,"}}
这是我试过的:
Labeled[images[[#]], Framed[text[[#]]],
LabelStyle ->
Directive[Bold, Alignment -> TextJustification,
Hyphenation -> False, FontFamily -> "Helvetica"]] & /@
Range[Length[images]]
我不会做的事情:
- 添加额外的注释框
- A4 尺寸
- 使图片和文字大小合适
- 断字似乎也不能正常工作
这是一个方法。首先为您选择的输出设置打印区域。 PDF 边距可能不同于直接从 Mathematica 打印。请注意,由于设置 $FrontEndSession
,这些设置仅适用于当前会话。
SetOptions[$FrontEndSession, PrintingOptions -> {
"PrintingMargins" -> {
{10(* position left *), 40(* clip right *)},
{30(* clip bottom *), 25(* position top *)}},
"FirstPageHeader" -> False,
"FirstPageFooter" -> False,
"RestPagesHeader" -> False,
"RestPagesFooter" -> False,
"Magnification" -> 1}];
testpage = Graphics[{White, Rectangle[{0, 0}, {700, 1080}], Black,
Line[{{0, 0}, {700 - 1, 0}, {700 - 1, 1080}, {0, 1080}, {0, 0}}]},
PlotRange -> {{0, 700}, {0, 1080}}, ImageSize -> 700];
nb = CreateDocument[ExpressionCell[testpage, "Print"], WindowSize -> 850];
Export[FileNameJoin[{$InitialDirectory, "testpage.pdf"}], nb];
NotebookClose[nb]
使用 OP 的图像和文本之一,修改为包含用于换行演示的连字符。
image = Import[FileNameJoin[{$InitialDirectory, "p1.jpg"}]];
text1 = StringRiffle[StringReplace[StringRiffle[#, " "], {". " -> ". ",
"some definitions" -> "some-definitions"}] & /@ Most@text, "\n\n"];
页面创建功能。
createPage[{image_, text_, number_, pdfname_}] := Module[{y = -200},
page = Graphics[{White, Rectangle[{0, 0}, {700, 1080}], Black,
Inset[image, {0, 1080}, {Left, Top}, {700, Automatic}],
Inset[
Graphics[{Blue, Thickness[0.005],
Line[{{0, 600 + 1 + y}, {700 - 1, 600 + 1 + y},
{700 - 1, 800 + y}, {0, 800 + y}, {0, 0}}],
Black,
Inset[
TextCell[text, LineSpacing -> {0, 16}, TextJustification -> 1],
Center, Center, {600, Automatic}]
},
PlotRange -> {{0, 700}, {600 + y, 800 + y}}, ImageSize -> 700,
BaseStyle -> {FontFamily -> "Times",
FontWeight -> "Plain", FontSize -> 15.5}],
{0, 0}, {0, 0}, {700, Automatic}],
Inset[
Graphics[{
Red,
Line[{{570, 100 + 1}, {630 - 1, 100 + 1},
{630 - 1, 160}, {570, 160}, {570, 100 + 1}}],
Black,
Inset[ToString[number], Center, Center, Automatic]
},
PlotRange -> {{570, 630}, {100, 160}}, ImageSize -> 60,
BaseStyle -> {FontFamily -> "Times",
FontWeight -> "Plain", FontSize -> 32}],
{0, 0}, {0, 0}, {60, Automatic}]
},
PlotRange -> {{0, 700}, {0, 1080}}, ImageSize -> 700,
BaseStyle -> {FontFamily -> "Times",
FontWeight -> "Plain", FontSize -> 15.5}];
nb = CreateDocument[ExpressionCell[page, "Print"], WindowSize -> 850];
Export[FileNameJoin[{$InitialDirectory, pdfname}], nb];
NotebookClose[nb]]
createPage[{image, text1, 4, "page.pdf"}]
注意。 +1
和 -1
调整仅适用于屏幕渲染。在打印或转换为 PDF 时可以省略它们。
估计文本块高度
如果您的文本插入页的高度因页面而异,则可以使用此例程以编程方式调整布局。请注意,这是近似值,因为它在 "Working"
screen style environment 中运行,而 PDF 或打印使用略有不同的 "Printout"
环境。例如,第一段在 "Printout"
环境中换行到四行。
makebox[wd_, ht_] := Graphics[{Green, Rectangle[{0, 0}, {wd, ht}],
Black, Inset[
TextCell[text1, LineSpacing -> {0, 16}, TextJustification -> 1],
{0, 0}, {Left, Bottom}, {wd, Automatic}]},
BaseStyle -> {FontFamily -> "Times",
FontWeight -> "Plain", FontSize -> 15.5},
PlotRange -> {{0, wd}, {0, ht}}, ImageSize -> wd]
box = makebox[600, 900];
raster = Rasterize[box, "Data"];
topline = raster[[1]] = raster[[2]];
newheight = Length[Reverse@raster /. {x__, topline ..} :> {x}] + 4;
Row[{"Text block pixel height is ", newheight}]
makebox[600, newheight]
多页 PDF
在单个 PDF 中发布多个页面。
nb = CreateDocument[{
ExpressionCell[page1, "Print"],
ExpressionCell[page2, "Print"],
ExpressionCell[page3, "Print"]}, WindowSize -> 850];
Export[FileNameJoin[{$InitialDirectory, "allpages.pdf"}], nb];
NotebookClose[nb]