Class: ReVIEW::LATEXBuilder

Inherits:
Builder show all
Includes:
LaTeXUtils, TextUtils
Defined in:
lib/review/latexbuilder.rb

Constant Summary collapse

HEADLINE =
{
  1 => 'chapter',
  2 => 'section',
  3 => 'subsection',
  4 => 'subsubsection',
  5 => 'paragraph',
  6 => 'subparagraph'
}.freeze
BOUTEN =
''.freeze

Constants inherited from Builder

Builder::CAPTION_TITLES

Instance Method Summary collapse

Methods included from TextUtils

#detab, #split_paragraph

Methods included from LaTeXUtils

#escape_index, #escape_latex, #escape_url, #initialize_metachars, #macro, #unescape_latex

Methods inherited from Builder

#bibpaper, #bind, #compile_inline, #detab, #embed, #error, #extract_chapter_id, #firstlinenum, #get_chap, #graph, #image, #include, #initialize, #inline_column, #inline_embed, #inline_hd, #inline_href, #inline_imgref, #inline_include, #inline_kw, #inline_ruby, #line_num, #parse_metric, #post_paragraph, #pre_paragraph, #raw, #result, #target_name, #text, #tsize, #ul_item_begin, #ul_item_end, #warn

Constructor Details

This class inherits a constructor from ReVIEW::Builder

Instance Method Details

#bibpaper_bibpaper(_id, _caption, lines) ⇒ Object



916
917
918
919
# File 'lib/review/latexbuilder.rb', line 916

def bibpaper_bibpaper(_id, _caption, lines)
  print split_paragraph(lines).join
  puts ''
end

#bibpaper_header(id, caption) ⇒ Object



911
912
913
914
# File 'lib/review/latexbuilder.rb', line 911

def bibpaper_header(id, caption)
  puts "[#{@chapter.bibpaper(id).number}] #{compile_inline(caption)}"
  puts macro('label', bib_label(id))
end

#box(lines, caption = nil) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/review/latexbuilder.rb', line 175

def box(lines, caption = nil)
  blank
  puts macro('reviewboxcaption', compile_inline(caption)) if caption
  puts '\begin{reviewbox}'
  lines.each { |line| puts detab(line) }
  puts '\end{reviewbox}'
  blank
end

#captionblock(_type, lines, caption) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/review/latexbuilder.rb', line 165

def captionblock(_type, lines, caption)
  puts "\\begin{reviewminicolumn}\n"
  puts "\\reviewminicolumntitle{#{compile_inline(caption)}}\n" if caption

  blocked_lines = split_paragraph(lines)
  puts blocked_lines.join("\n\n")

  puts "\\end{reviewminicolumn}\n"
end

#center(lines) ⇒ Object Also known as: centering



617
618
619
# File 'lib/review/latexbuilder.rb', line 617

def center(lines)
  latex_block 'center', lines
end

#cmd(lines, caption = nil, lang = nil) ⇒ Object



298
299
300
301
302
303
304
305
# File 'lib/review/latexbuilder.rb', line 298

def cmd(lines, caption = nil, lang = nil)
  if highlight_listings?
    common_code_block_lst(nil, lines, 'reviewcmdlst', 'title', caption, lang)
  else
    blank
    common_code_block(nil, lines, 'reviewcmd', caption, lang) { |line, _idx| detab(line) + "\n" }
  end
end

#column_begin(level, label, caption) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/review/latexbuilder.rb', line 148

def column_begin(level, label, caption)
  blank
  puts "\\begin{reviewcolumn}\n"
  if label
    puts "\\hypertarget{#{column_label(label)}}{}"
  else
    puts "\\hypertarget{#{column_label(caption)}}{}"
  end
  puts macro('reviewcolumnhead', nil, compile_inline(caption))
  puts "\\addcontentsline{toc}{#{HEADLINE[level]}}{#{compile_inline(caption)}}" if level <= @book.config['toclevel'].to_i
end

#column_end(_level) ⇒ Object



160
161
162
163
# File 'lib/review/latexbuilder.rb', line 160

def column_end(_level)
  puts "\\end{reviewcolumn}\n"
  blank
end

#comment(lines, comment = nil) ⇒ Object



650
651
652
653
654
655
656
# File 'lib/review/latexbuilder.rb', line 650

def comment(lines, comment = nil)
  lines ||= []
  lines.unshift comment unless comment.blank?
  return true unless @book.config['draft']
  str = lines.join('\par ')
  puts macro('pdfcomment', escape(str))
end

#common_code_block(id, lines, command, caption, _lang) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/review/latexbuilder.rb', line 307

def common_code_block(id, lines, command, caption, _lang)
  if caption
    if command =~ /emlist/ || command =~ /cmd/ || command =~ /source/
      puts macro(command + 'caption', compile_inline(caption))
    else
      begin
        if get_chap.nil?
          puts macro('reviewlistcaption', "#{I18n.t('list')}#{I18n.t('format_number_header_without_chapter', [@chapter.list(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}")
        else
          puts macro('reviewlistcaption', "#{I18n.t('list')}#{I18n.t('format_number_header', [get_chap, @chapter.list(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}")
        end
      rescue KeyError
        error "no such list: #{id}"
      end
    end
  end
  body = ''
  lines.each_with_index { |line, idx| body.concat(yield(line, idx)) }
  puts macro('begin', command)
  print body
  puts macro('end', command)
  blank
end

#common_code_block_lst(_id, lines, command, title, caption, lang, first_line_num: 1) ⇒ Object



331
332
333
334
335
336
337
338
339
# File 'lib/review/latexbuilder.rb', line 331

def common_code_block_lst(_id, lines, command, title, caption, lang, first_line_num: 1)
  print '\vspace{-1.5em}' if title == 'title' && caption.blank?
  body = lines.inject('') { |i, j| i + detab(unescape_latex(j)) + "\n" }
  args = make_code_block_args(title, caption, lang, first_line_num: first_line_num)
  puts %Q(\\begin{#{command}}[#{args}])
  print body
  puts %Q(\\end{#{command}})
  blank
end

#compile_href(url, label) ⇒ Object



953
954
955
956
957
958
959
960
961
962
963
# File 'lib/review/latexbuilder.rb', line 953

def compile_href(url, label)
  if /\A[a-z]+:/ =~ url
    if label
      macro('href', escape_url(url), escape(label))
    else
      macro('url', escape_url(url))
    end
  else
    macro('ref', url)
  end
end

#compile_kw(word, alt) ⇒ Object



945
946
947
948
949
950
951
# File 'lib/review/latexbuilder.rb', line 945

def compile_kw(word, alt)
  if alt
    macro('reviewkw', escape(word)) + "#{escape(alt.strip)}"
  else
    macro('reviewkw', escape(word))
  end
end

#compile_ruby(base, ruby) ⇒ Object



763
764
765
# File 'lib/review/latexbuilder.rb', line 763

def compile_ruby(base, ruby)
  macro('ruby', escape(base), escape(ruby))
end

#dd(lines) ⇒ Object



230
231
232
# File 'lib/review/latexbuilder.rb', line 230

def dd(lines)
  puts lines.join
end

#direct(lines, fmt) ⇒ Object



645
646
647
648
# File 'lib/review/latexbuilder.rb', line 645

def direct(lines, fmt)
  return unless fmt == 'latex'
  lines.each { |line| puts line }
end

#dl_beginObject



219
220
221
222
# File 'lib/review/latexbuilder.rb', line 219

def dl_begin
  blank
  puts '\begin{description}'
end

#dl_endObject



234
235
236
237
# File 'lib/review/latexbuilder.rb', line 234

def dl_end
  puts '\end{description}'
  blank
end

#dt(str) ⇒ Object



224
225
226
227
228
# File 'lib/review/latexbuilder.rb', line 224

def dt(str)
  str.sub!(/\[/) { '\lbrack{}' }
  str.sub!(/\]/) { '\rbrack{}' }
  puts '\item[' + str + '] \mbox{} \\\\'
end

#emlist(lines, caption = nil, lang = nil) ⇒ Object



260
261
262
263
264
265
266
267
# File 'lib/review/latexbuilder.rb', line 260

def emlist(lines, caption = nil, lang = nil)
  blank
  if highlight_listings?
    common_code_block_lst(nil, lines, 'reviewemlistlst', 'title', caption, lang)
  else
    common_code_block(nil, lines, 'reviewemlist', caption, lang) { |line, _idx| detab(line) + "\n" }
  end
end

#emlistnum(lines, caption = nil, lang = nil) ⇒ Object



269
270
271
272
273
274
275
276
277
# File 'lib/review/latexbuilder.rb', line 269

def emlistnum(lines, caption = nil, lang = nil)
  blank
  first_line_num = line_num
  if highlight_listings?
    common_code_block_lst(nil, lines, 'reviewemlistnumlst', 'title', caption, lang, first_line_num: first_line_num)
  else
    common_code_block(nil, lines, 'reviewemlist', caption, lang) { |line, idx| detab((idx + first_line_num).to_s.rjust(2) + ': ' + line) + "\n" }
  end
end

#emtable(lines, caption = nil) ⇒ Object



573
574
575
# File 'lib/review/latexbuilder.rb', line 573

def emtable(lines, caption = nil)
  table(lines, nil, caption)
end

#extnameObject



24
25
26
# File 'lib/review/latexbuilder.rb', line 24

def extname
  '.tex'
end

#flushright(lines) ⇒ Object



623
624
625
# File 'lib/review/latexbuilder.rb', line 623

def flushright(lines)
  latex_block 'flushright', lines
end

#footnote(id, content) ⇒ Object



745
746
747
# File 'lib/review/latexbuilder.rb', line 745

def footnote(id, content)
  puts macro("footnotetext[#{@chapter.footnote(id).number}]", compile_inline(content.strip)) if @book.config['footnotetext']
end

#handle_metric(str) ⇒ Object



368
369
370
371
372
373
# File 'lib/review/latexbuilder.rb', line 368

def handle_metric(str)
  if @book.config['image_scale2width'] && str =~ /\Ascale=([\d.]+)\Z/
    return "width=#{$1}\\maxwidth"
  end
  str
end

#headline(level, label, caption) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/review/latexbuilder.rb', line 99

def headline(level, label, caption)
  _, anchor = headline_prefix(level)
  headline_name = HEADLINE[level]
  headline_name = 'part' if @chapter.is_a? ReVIEW::Book::Part
  prefix = if level > @book.config['secnolevel'] || (@chapter.number.to_s.empty? && level > 1)
             '*'
           else
             ''
           end
  blank unless @output.pos == 0
  puts macro(headline_name + prefix, compile_inline(caption))
  puts "\\addcontentsline{toc}{#{headline_name}}{#{compile_inline(caption)}}" if prefix == '*' && level <= @book.config['toclevel'].to_i
  if level == 1
    puts macro('label', chapter_label)
  else
    puts macro('label', sec_label(anchor))
    puts macro('label', label) if label
  end
rescue
  error "unknown level: #{level}"
end

#hrObject



658
659
660
# File 'lib/review/latexbuilder.rb', line 658

def hr
  puts '\hrule'
end

#image_dummy(id, caption, lines) ⇒ Object



393
394
395
396
397
398
399
400
401
402
# File 'lib/review/latexbuilder.rb', line 393

def image_dummy(id, caption, lines)
  warn "image not bound: #{id}"
  puts '\begin{reviewdummyimage}'
  # path = @chapter.image(id).path
  puts "--[[path = #{id} (#{existence(id)})]]--"
  lines.each { |line| puts detab(line.rstrip) }
  puts macro('label', image_label(id))
  puts compile_inline(caption)
  puts '\end{reviewdummyimage}'
end

#image_extObject



969
970
971
# File 'lib/review/latexbuilder.rb', line 969

def image_ext
  'pdf'
end

#image_header(id, caption) ⇒ Object



365
366
# File 'lib/review/latexbuilder.rb', line 365

def image_header(id, caption)
end

#image_image(id, caption, metric) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/review/latexbuilder.rb', line 379

def image_image(id, caption, metric)
  metrics = parse_metric('latex', metric)
  # image is always bound here
  puts '\begin{reviewimage}'
  if metrics.present?
    puts "\\includegraphics[#{metrics}]{#{@chapter.image(id).path}}"
  else
    puts "\\includegraphics[width=\\maxwidth]{#{@chapter.image(id).path}}"
  end
  puts macro('caption', compile_inline(caption)) if caption.present?
  puts macro('label', image_label(id))
  puts '\end{reviewimage}'
end

#imgtable(lines, id, caption = nil, metric = nil) ⇒ Object



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/review/latexbuilder.rb', line 577

def imgtable(lines, id, caption = nil, metric = nil)
  unless @chapter.image(id).bound?
    warn "image not bound: #{id}"
    image_dummy id, caption, lines
    return
  end

  begin
    if caption.present?
      @table_caption = true
      puts '\begin{table}[h]'
      puts macro('reviewimgtablecaption', compile_inline(caption))
    end
    puts macro('label', table_label(id))
  rescue ReVIEW::KeyError
    error "no such table: #{id}"
  end
  imgtable_image(id, caption, metric)

  puts '\end{table}' if @table_caption
  @table_caption = nil
  blank
end

#imgtable_image(id, _caption, metric) ⇒ Object



601
602
603
604
605
606
607
608
609
610
611
# File 'lib/review/latexbuilder.rb', line 601

def imgtable_image(id, _caption, metric)
  metrics = parse_metric('latex', metric)
  # image is always bound here
  puts '\begin{reviewimage}'
  if metrics.present?
    puts "\\includegraphics[#{metrics}]{#{@chapter.image(id).path}}"
  else
    puts "\\includegraphics[width=\\maxwidth]{#{@chapter.image(id).path}}"
  end
  puts '\end{reviewimage}'
end

#indepimage(lines, id, caption = nil, metric = nil) ⇒ Object Also known as: numberlessimage



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/review/latexbuilder.rb', line 443

def indepimage(lines, id, caption = nil, metric = nil)
  metrics = parse_metric('latex', metric)

  if @chapter.image(id).path
    puts '\begin{reviewimage}'
    if metrics.present?
      puts "\\includegraphics[#{metrics}]{#{@chapter.image(id).path}}"
    else
      puts "\\includegraphics[width=\\maxwidth]{#{@chapter.image(id).path}}"
    end
  else
    warn "image not bound: #{id}"
    puts '\begin{reviewdummyimage}'
    puts "--[[path = #{id} (#{existence(id)})]]--"
    lines.each { |line| puts detab(line.rstrip) }
  end

  puts macro('reviewindepimagecaption', %Q(#{I18n.t('numberless_image')}#{I18n.t('caption_prefix')}#{compile_inline(caption)})) if caption.present?

  if @chapter.image(id).path
    puts '\end{reviewimage}'
  else
    puts '\end{reviewdummyimage}'
  end
end

#index(str) ⇒ Object



921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
# File 'lib/review/latexbuilder.rb', line 921

def index(str)
  sa = str.split('<<>>')

  sa.map! do |item|
    if @index_db[item]
      escape_index(escape_latex(@index_db[item])) + '@' + escape_index(escape_latex(item))
    else
      if item =~ /\A[[:ascii:]]+\Z/ || @index_mecab.nil?
        esc_item = escape_index(escape_latex(item))
        if esc_item != item
          "#{escape_index(item)}@#{esc_item}"
        else
          esc_item
        end
      else
        yomi = NKF.nkf('-w --hiragana', @index_mecab.parse(item).force_encoding('UTF-8').chomp)
        escape_index(escape_latex(yomi)) + '@' + escape_index(escape_latex(item))
      end
    end
  end

  "\\index{#{sa.join('!')}}"
end

#inline_ami(str) ⇒ Object



881
882
883
# File 'lib/review/latexbuilder.rb', line 881

def inline_ami(str)
  macro('reviewami', escape(str))
end

#inline_b(str) ⇒ Object

bold



793
794
795
# File 'lib/review/latexbuilder.rb', line 793

def inline_b(str)
  macro('textbf', escape(str))
end

#inline_bib(id) ⇒ Object



832
833
834
# File 'lib/review/latexbuilder.rb', line 832

def inline_bib(id)
  macro('reviewbibref', "[#{@chapter.bibpaper(id).number}]", bib_label(id))
end

#inline_bou(str) ⇒ Object



759
760
761
# File 'lib/review/latexbuilder.rb', line 759

def inline_bou(str)
  str.split(//).map { |c| macro('ruby', escape(c), macro('textgt', BOUTEN)) }.join('\allowbreak')
end

#inline_br(_str) ⇒ Object

line break



798
799
800
# File 'lib/review/latexbuilder.rb', line 798

def inline_br(_str)
  "\\\\\n"
end

#inline_chap(id) ⇒ Object



690
691
692
693
694
695
696
697
698
699
# File 'lib/review/latexbuilder.rb', line 690

def inline_chap(id)
  if @book.config['chapterlink']
    "\\hyperref[chap:#{id}]{#{@book.chapter_index.number(id)}}"
  else
    @book.chapter_index.number(id)
  end
rescue KeyError
  error "unknown chapter: #{id}"
  nofunc_text("[UnknownChapter:#{id}]")
end

#inline_chapref(id) ⇒ Object



678
679
680
681
682
683
684
685
686
687
688
# File 'lib/review/latexbuilder.rb', line 678

def inline_chapref(id)
  title = super
  if @book.config['chapterlink']
    "\\hyperref[chap:#{id}]{#{title}}"
  else
    title
  end
rescue KeyError
  error "unknown chapter: #{id}"
  nofunc_text("[UnknownChapter:#{id}]")
end

#inline_code(str) ⇒ Object

@<code> is same as @<tt>



808
809
810
# File 'lib/review/latexbuilder.rb', line 808

def inline_code(str)
  macro('texttt', escape(str))
end

#inline_column_chap(chapter, id) ⇒ Object



851
852
853
854
855
# File 'lib/review/latexbuilder.rb', line 851

def inline_column_chap(chapter, id)
  macro('reviewcolumnref',
        I18n.t('chapter_quote', compile_inline(chapter.column(id).caption)),
        column_label(id, chapter))
end

#inline_comment(str) ⇒ Object



899
900
901
902
903
904
905
# File 'lib/review/latexbuilder.rb', line 899

def inline_comment(str)
  if @book.config['draft']
    macro('pdfcomment', escape(str))
  else
    ''
  end
end

#inline_del(str) ⇒ Object



820
821
822
# File 'lib/review/latexbuilder.rb', line 820

def inline_del(str)
  macro('reviewstrike', escape(str))
end

#inline_dtp(_str) ⇒ Object



802
803
804
805
# File 'lib/review/latexbuilder.rb', line 802

def inline_dtp(_str)
  # ignore
  ''
end

#inline_em(str) ⇒ Object



869
870
871
# File 'lib/review/latexbuilder.rb', line 869

def inline_em(str)
  macro('reviewem', escape(str))
end

#inline_fn(id) ⇒ Object



749
750
751
752
753
754
755
# File 'lib/review/latexbuilder.rb', line 749

def inline_fn(id)
  if @book.config['footnotetext']
    macro("footnotemark[#{@chapter.footnote(id).number}]", '')
  else
    macro('footnote', compile_inline(@chapter.footnote(id).content.strip))
  end
end

#inline_hd_chap(chap, id) ⇒ Object



836
837
838
839
840
841
842
843
844
845
846
847
848
849
# File 'lib/review/latexbuilder.rb', line 836

def inline_hd_chap(chap, id)
  n = chap.headline_index.number(id)
  if chap.number and @book.config['secnolevel'] >= n.split('.').size
    str = I18n.t('chapter_quote', "#{chap.headline_index.number(id)} #{compile_inline(chap.headline(id).caption)}")
  else
    str = I18n.t('chapter_quote', compile_inline(chap.headline(id).caption))
  end
  if @book.config['chapterlink']
    anchor = n.gsub(/\./, '-')
    macro('reviewsecref', str, sec_label(anchor))
  else
    str
  end
end

#inline_hi(str) ⇒ Object

hidden index



773
774
775
# File 'lib/review/latexbuilder.rb', line 773

def inline_hi(str)
  index(str)
end

#inline_hidx(str) ⇒ Object

hidden index



788
789
790
# File 'lib/review/latexbuilder.rb', line 788

def inline_hidx(str)
  index(str)
end

#inline_i(str) ⇒ Object

index -> italic



778
779
780
# File 'lib/review/latexbuilder.rb', line 778

def inline_i(str)
  macro('textit', escape(str))
end

#inline_icon(id) ⇒ Object



885
886
887
888
889
890
891
892
# File 'lib/review/latexbuilder.rb', line 885

def inline_icon(id)
  if @chapter.image(id).path
    macro('includegraphics', @chapter.image(id).path)
  else
    warn "image not bound: #{id}"
    "\\verb|--[[path = #{id} (#{existence(id)})]]--|"
  end
end

#inline_idx(str) ⇒ Object

index



783
784
785
# File 'lib/review/latexbuilder.rb', line 783

def inline_idx(str)
  escape(str) + index(str)
end

#inline_img(id) ⇒ Object



736
737
738
739
740
741
742
743
# File 'lib/review/latexbuilder.rb', line 736

def inline_img(id)
  chapter, id = extract_chapter_id(id)
  if get_chap(chapter).nil?
    macro('reviewimageref', I18n.t('format_number_without_chapter', [chapter.image(id).number]), image_label(id, chapter))
  else
    macro('reviewimageref', I18n.t('format_number', [get_chap(chapter), chapter.image(id).number]), image_label(id, chapter))
  end
end

#inline_list(id) ⇒ Object

FIXME: use TeX native label/ref.



718
719
720
721
722
723
724
725
# File 'lib/review/latexbuilder.rb', line 718

def inline_list(id)
  chapter, id = extract_chapter_id(id)
  if get_chap(chapter).nil?
    macro('reviewlistref', I18n.t('format_number_without_chapter', [chapter.list(id).number]))
  else
    macro('reviewlistref', I18n.t('format_number', [get_chap(chapter), chapter.list(id).number]))
  end
end

#inline_m(str) ⇒ Object

math



768
769
770
# File 'lib/review/latexbuilder.rb', line 768

def inline_m(str)
  " $#{str}$ "
end

#inline_pageref(id) ⇒ Object



713
714
715
# File 'lib/review/latexbuilder.rb', line 713

def inline_pageref(id)
  "\\pageref{#{id}}"
end

#inline_raw(str) ⇒ Object



857
858
859
# File 'lib/review/latexbuilder.rb', line 857

def inline_raw(str)
  super(str)
end

#inline_strong(str) ⇒ Object



873
874
875
# File 'lib/review/latexbuilder.rb', line 873

def inline_strong(str)
  macro('reviewstrong', escape(str))
end

#inline_sub(str) ⇒ Object



861
862
863
# File 'lib/review/latexbuilder.rb', line 861

def inline_sub(str)
  macro('textsubscript', escape(str))
end

#inline_sup(str) ⇒ Object



865
866
867
# File 'lib/review/latexbuilder.rb', line 865

def inline_sup(str)
  macro('textsuperscript', escape(str))
end

#inline_table(id) ⇒ Object



727
728
729
730
731
732
733
734
# File 'lib/review/latexbuilder.rb', line 727

def inline_table(id)
  chapter, id = extract_chapter_id(id)
  if get_chap(chapter).nil?
    macro('reviewtableref', I18n.t('format_number_without_chapter', [chapter.table(id).number]), table_label(id, chapter))
  else
    macro('reviewtableref', I18n.t('format_number', [get_chap(chapter), chapter.table(id).number]), table_label(id, chapter))
  end
end

#inline_tcy(str) ⇒ Object



907
908
909
# File 'lib/review/latexbuilder.rb', line 907

def inline_tcy(str)
  macro('rensuji', escape(str))
end

#inline_title(id) ⇒ Object



701
702
703
704
705
706
707
708
709
710
711
# File 'lib/review/latexbuilder.rb', line 701

def inline_title(id)
  title = super
  if @book.config['chapterlink']
    "\\hyperref[chap:#{id}]{#{title}}"
  else
    title
  end
rescue KeyError
  error "unknown chapter: #{id}"
  nofunc_text("[UnknownChapter:#{id}]")
end

#inline_tt(str) ⇒ Object



816
817
818
# File 'lib/review/latexbuilder.rb', line 816

def inline_tt(str)
  macro('texttt', escape(str))
end

#inline_ttb(str) ⇒ Object



828
829
830
# File 'lib/review/latexbuilder.rb', line 828

def inline_ttb(str)
  macro('texttt', macro('textbf', escape(str)))
end

#inline_tti(str) ⇒ Object



824
825
826
# File 'lib/review/latexbuilder.rb', line 824

def inline_tti(str)
  macro('texttt', macro('textit', escape(str)))
end

#inline_u(str) ⇒ Object



877
878
879
# File 'lib/review/latexbuilder.rb', line 877

def inline_u(str)
  macro('reviewunderline', escape(str))
end

#inline_uchar(str) ⇒ Object



894
895
896
897
# File 'lib/review/latexbuilder.rb', line 894

def inline_uchar(str)
  # with otf package
  macro('UTF', escape(str))
end

#label(id) ⇒ Object



662
663
664
# File 'lib/review/latexbuilder.rb', line 662

def label(id)
  puts macro('label', id)
end

#latextsize(str) ⇒ Object



965
966
967
# File 'lib/review/latexbuilder.rb', line 965

def latextsize(str)
  @latex_tsize = str
end

#linebreakObject



670
671
672
# File 'lib/review/latexbuilder.rb', line 670

def linebreak
  puts '\\\\'
end

#list(lines, id, caption, lang = nil) ⇒ Object

override Builder#list



280
281
282
283
284
285
286
# File 'lib/review/latexbuilder.rb', line 280

def list(lines, id, caption, lang = nil)
  if highlight_listings?
    common_code_block_lst(id, lines, 'reviewlistlst', 'caption', caption, lang)
  else
    common_code_block(id, lines, 'reviewlist', caption, lang) { |line, _idx| detab(line) + "\n" }
  end
end

#listnum(lines, id, caption, lang = nil) ⇒ Object

override Builder#listnum



289
290
291
292
293
294
295
296
# File 'lib/review/latexbuilder.rb', line 289

def listnum(lines, id, caption, lang = nil)
  first_line_num = line_num
  if highlight_listings?
    common_code_block_lst(id, lines, 'reviewlistnumlst', 'caption', caption, lang, first_line_num: first_line_num)
  else
    common_code_block(id, lines, 'reviewlist', caption, lang) { |line, idx| detab((idx + first_line_num).to_s.rjust(2) + ': ' + line) + "\n" }
  end
end

#load_idxdb(file) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/review/latexbuilder.rb', line 58

def load_idxdb(file)
  table = {}
  File.foreach(file) do |line|
    key, value = *line.strip.split(/\t+/, 2)
    table[key] = value
  end
  table
end

#make_code_block_args(title, caption, lang, first_line_num: 1) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/review/latexbuilder.rb', line 341

def make_code_block_args(title, caption, lang, first_line_num: 1)
  caption_str = compile_inline((caption || ''))
  if title == 'title' && caption_str == ''
    caption_str = '\relax' ## dummy charactor to remove lstname
  end
  lexer = if @book.config['highlight'] && @book.config['highlight']['lang']
            @book.config['highlight']['lang'] # default setting
          else
            ''
          end
  lexer = lang if lang.present?
  args = %Q(#{title}={#{caption_str}},language={#{lexer}})
  args += ",firstnumber=#{first_line_num}" if first_line_num != 1
  args
end

#nodisp_begin(level, _label, caption) ⇒ Object



138
139
140
141
142
143
# File 'lib/review/latexbuilder.rb', line 138

def nodisp_begin(level, _label, caption)
  blank unless @output.pos == 0
  puts macro('clearpage') if @output.pos == 0
  puts macro('addcontentsline', 'toc', HEADLINE[level], compile_inline(caption))
  # FIXME: headings
end

#nodisp_end(level) ⇒ Object



145
146
# File 'lib/review/latexbuilder.rb', line 145

def nodisp_end(level)
end

#nofunc_text(str) ⇒ Object



812
813
814
# File 'lib/review/latexbuilder.rb', line 812

def nofunc_text(str)
  escape(str)
end

#noindentObject



674
675
676
# File 'lib/review/latexbuilder.rb', line 674

def noindent
  print '\noindent'
end

#nonum_begin(level, _label, caption) ⇒ Object



121
122
123
124
125
# File 'lib/review/latexbuilder.rb', line 121

def nonum_begin(level, _label, caption)
  blank unless @output.pos == 0
  puts macro(HEADLINE[level] + '*', compile_inline(caption))
  puts macro('addcontentsline', 'toc', HEADLINE[level], compile_inline(caption))
end

#nonum_end(level) ⇒ Object



127
128
# File 'lib/review/latexbuilder.rb', line 127

def nonum_end(level)
end

#notoc_begin(level, _label, caption) ⇒ Object



130
131
132
133
# File 'lib/review/latexbuilder.rb', line 130

def notoc_begin(level, _label, caption)
  blank unless @output.pos == 0
  puts macro(HEADLINE[level] + '*', compile_inline(caption))
end

#notoc_end(level) ⇒ Object



135
136
# File 'lib/review/latexbuilder.rb', line 135

def notoc_end(level)
end

#ol_beginObject



200
201
202
203
204
205
206
# File 'lib/review/latexbuilder.rb', line 200

def ol_begin
  blank
  puts '\begin{enumerate}'
  return true unless @ol_num
  puts "\\setcounter{enumi}{#{@ol_num - 1}}"
  @ol_num = nil
end

#ol_endObject



214
215
216
217
# File 'lib/review/latexbuilder.rb', line 214

def ol_end
  puts '\end{enumerate}'
  blank
end

#ol_item(lines, _num) ⇒ Object



208
209
210
211
212
# File 'lib/review/latexbuilder.rb', line 208

def ol_item(lines, _num)
  str = lines.join
  str.sub!(/\A(\[)/) { '\lbrack{}' }
  puts '\item ' + str
end

#olnum(num) ⇒ Object



973
974
975
# File 'lib/review/latexbuilder.rb', line 973

def olnum(num)
  @ol_num = num.to_i
end

#pagebreakObject



666
667
668
# File 'lib/review/latexbuilder.rb', line 666

def pagebreak
  puts '\pagebreak'
end

#paragraph(lines) ⇒ Object



239
240
241
242
243
# File 'lib/review/latexbuilder.rb', line 239

def paragraph(lines)
  blank
  lines.each { |line| puts line }
  blank
end

#parasepObject



245
246
247
# File 'lib/review/latexbuilder.rb', line 245

def parasep
  puts '\\parasep'
end

#quote(lines) ⇒ Object



613
614
615
# File 'lib/review/latexbuilder.rb', line 613

def quote(lines)
  latex_block 'quote', lines
end

#read(lines) ⇒ Object Also known as: lead



249
250
251
# File 'lib/review/latexbuilder.rb', line 249

def read(lines)
  latex_block 'quotation', lines
end

#result_metric(array) ⇒ Object



375
376
377
# File 'lib/review/latexbuilder.rb', line 375

def result_metric(array)
  array.join(',')
end

#setup_indexObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/review/latexbuilder.rb', line 42

def setup_index
  @index_db = {}
  @index_mecab = nil
  return true unless @book.config['pdfmaker']['makeindex']

  @index_db = load_idxdb(@book.config['pdfmaker']['makeindex_dic']) if @book.config['pdfmaker']['makeindex_dic']
  return true unless @book.config['pdfmaker']['makeindex_mecab']
  begin
    require 'MeCab'
    require 'nkf'
    @index_mecab = MeCab::Tagger.new(@book.config['pdfmaker']['makeindex_mecab_opts'])
  rescue LoadError
    error 'not found MeCab'
  end
end

#source(lines, caption = nil, lang = nil) ⇒ Object



357
358
359
360
361
362
363
# File 'lib/review/latexbuilder.rb', line 357

def source(lines, caption = nil, lang = nil)
  if highlight_listings?
    common_code_block_lst(nil, lines, 'reviewsourcelst', 'title', caption, lang)
  else
    common_code_block(nil, lines, 'reviewsource', caption, lang) { |line, _idx| detab(line) + "\n" }
  end
end

#table(lines, id = nil, caption = nil) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/review/latexbuilder.rb', line 471

def table(lines, id = nil, caption = nil)
  rows = []
  sepidx = nil
  lines.each_with_index do |line, idx|
    if /\A[\=\{\-\}]{12}/ =~ line
      # just ignore
      # error "too many table separator" if sepidx
      sepidx ||= idx
      next
    end
    rows.push(line.strip.split(/\t+/).map { |s| s.sub(/\A\./, '') })
  end
  rows = adjust_n_cols(rows)

  begin
    table_header id, caption unless caption.nil?
  rescue KeyError
    error "no such table: #{id}"
  end
  return if rows.empty?
  table_begin rows.first.size
  if sepidx
    sepidx.times { tr(rows.shift.map { |s| th(s) }) }
    rows.each { |cols| tr(cols.map { |s| td(s) }) }
  else
    rows.each do |cols|
      h, *cs = *cols
      tr([th(h)] + cs.map { |s| td(s) })
    end
  end
  table_end
end

#table_begin(ncols) ⇒ Object



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/review/latexbuilder.rb', line 521

def table_begin(ncols)
  if @latex_tsize
    puts macro('begin', 'reviewtable', @latex_tsize)
  elsif @tsize
    if @tsize =~ /\A[\d., ]+\Z/
      cellwidth = @tsize.split(/\s*,\s*/)
      puts macro('begin', 'reviewtable', '|' + cellwidth.collect { |i| "p{#{i}mm}" }.join('|') + '|')
    else
      puts macro('begin', 'reviewtable', @tsize)
    end
  else
    puts macro('begin', 'reviewtable', (['|'] * (ncols + 1)).join('l'))
  end
  puts '\hline'
  @tsize = nil
  @latex_tsize = nil
end

#table_endObject



566
567
568
569
570
571
# File 'lib/review/latexbuilder.rb', line 566

def table_end
  puts macro('end', 'reviewtable')
  puts '\end{table}' if @table_caption
  @table_caption = nil
  blank
end

#table_header(id, caption) ⇒ Object



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/review/latexbuilder.rb', line 504

def table_header(id, caption)
  if id.nil?
    if caption.present?
      @table_caption = true
      puts '\begin{table}[h]'
      puts macro('reviewtablecaption*', compile_inline(caption))
    end
  else
    if caption.present?
      @table_caption = true
      puts '\begin{table}[h]'
      puts macro('reviewtablecaption', compile_inline(caption))
    end
    puts macro('label', table_label(id))
  end
end

#table_separatorObject



539
540
541
# File 'lib/review/latexbuilder.rb', line 539

def table_separator
  # puts '\hline'
end

#td(s) ⇒ Object



552
553
554
555
556
557
558
559
# File 'lib/review/latexbuilder.rb', line 552

def td(s)
  ## use shortstack for @<br>
  if /\\\\/ =~ s
    macro('shortstack[l]', s)
  else
    s
  end
end

#texequation(lines) ⇒ Object



627
628
629
630
631
632
633
# File 'lib/review/latexbuilder.rb', line 627

def texequation(lines)
  blank
  puts macro('begin', 'equation*')
  lines.each { |line| puts unescape_latex(line) }
  puts macro('end', 'equation*')
  blank
end

#th(s) ⇒ Object



543
544
545
546
547
548
549
550
# File 'lib/review/latexbuilder.rb', line 543

def th(s)
  ## use shortstack for @<br>
  if /\\\\/i =~ s
    macro('reviewth', macro('shortstack[l]', s))
  else
    macro('reviewth', s)
  end
end

#tr(rows) ⇒ Object



561
562
563
564
# File 'lib/review/latexbuilder.rb', line 561

def tr(rows)
  print rows.join(' & ')
  puts ' \\\\  \hline'
end

#ul_beginObject



184
185
186
187
# File 'lib/review/latexbuilder.rb', line 184

def ul_begin
  blank
  puts '\begin{itemize}'
end

#ul_endObject



195
196
197
198
# File 'lib/review/latexbuilder.rb', line 195

def ul_end
  puts '\end{itemize}'
  blank
end

#ul_item(lines) ⇒ Object



189
190
191
192
193
# File 'lib/review/latexbuilder.rb', line 189

def ul_item(lines)
  str = lines.join
  str.sub!(/\A(\[)/) { '\lbrack{}' }
  puts '\item ' + str
end